0

我正在尝试从 17001 行 × 6 列 DataFrame中绘制life_expect['CountryName']预期寿命 超过 80 岁的国家。life_expect['Value'] 处理和绘图所需的时间超过 30 分钟。我做错了什么,如何使代码更快?

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats

indicators = pd.read_csv('Indicators.csv')

year = indicators['Year']
hist_indicator = indicators['IndicatorName'].str.contains('Life expectancy at birth')
le = indicators[year & hist_indicator]
x = le[~le['CountryName'].str.contains('OECD')]
x = x[~x['CountryName'].str.contains('income')]
x = x[~x['CountryName'].str.contains('Euro')]
life_expect = x

highest_life_expect = life_expect[life_expect['Value'] > 80]
sns.set_style("darkgrid")
sns.catplot(x = 'CountryName', y = 'Value', hue = 'Year', row='IndicatorName', data = highest_life_expect, palette = 'YlOrBr', kind = 'strip', dodge = True, height = 1.5, aspect=3)
plt.xticks(rotation= 70)

plt.show() 

数据帧示例:

life_expect = pd.DataFrame({'CountryName': {23215: 'Arab World', 23216: 'Arab World', 23217: 'Arab World', 23311: 'Caribbean small states', 23312: 'Caribbean small states', 23313: 'Caribbean small states', 23657: 'East Asia & Pacific (developing only)', 23658: 'East Asia & Pacific (developing only)', 23659: 'East Asia & Pacific (developing only)', 24249: 'Fragile and conflict affected situations'}, 'CountryCode': {23215: 'ARB', 23216: 'ARB', 23217: 'ARB', 23311: 'CSS', 23312: 'CSS', 23313: 
'CSS', 23657: 'EAP', 23658: 'EAP', 23659: 'EAP', 24249: 'FCS'}, 'IndicatorName': {23215: 'Life expectancy at birth, female (years)', 23216: 'Life expectancy at birth, male (years)', 23217: 'Life expectancy at birth, total (years)', 23311: 'Life expectancy at birth, female (years)', 23312: 'Life expectancy at birth, male (years)', 23313: 'Life expectancy at birth, total (years)', 23657: 'Life expectancy at birth, female (years)', 23658: 'Life expectancy at birth, male (years)', 23659: 'Life expectancy at birth, total (years)', 24249: 'Life expectancy at birth, female (years)'}, 'IndicatorCode': {23215: 'SP.DYN.LE00.FE.IN', 23216: 'SP.DYN.LE00.MA.IN', 23217: 'SP.DYN.LE00.IN', 23311: 'SP.DYN.LE00.FE.IN', 23312: 'SP.DYN.LE00.MA.IN', 23313: 'SP.DYN.LE00.IN', 23657: 'SP.DYN.LE00.FE.IN', 23658: 'SP.DYN.LE00.MA.IN', 23659: 'SP.DYN.LE00.IN', 24249: 'SP.DYN.LE00.FE.IN'}, 'Year': {23215: 1961, 23216: 1961, 23217: 1961, 23311: 1961, 23312: 1961, 23313: 1961, 23657: 1961, 23658: 1961, 23659: 1961, 24249: 1961}, 'Value': {23215: 48.461242672561596, 23216: 46.445430867169, 23217: 47.4276465406613, 23311: 64.82771091890339, 23312: 60.810503390166595, 23313: 62.7689979262271, 23657: 47.95864452502961, 23658: 44.11634167689369, 23659: 45.98732426589021, 24249: 43.4402244401312}})
4

0 回答 0