我的数据集看起来像这样。
"Name of Countries","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010"
"BANGLADESH","431312","435867","454611","477446","456371","484401","480240","541884","468899","431962"
"SRILANKA","112813","108008","109098","128711","136400","154813","204084","218805","239995","266515"
"UK","405472","387846","430917","555907","651803","734240","796191","776530","769251","759494"
"USA","329147","348182","410803","526120","611165","696739","799062","804933","827140","931292"
我想用y轴绘制行作为值,x轴是年份,例如。我试过美国
t=df[df['Name of Countries']=='USA']
x=pd.DataFrame([t.iloc[0].index,t.iloc[0].values]).T
x.plot()
plt.show()
我希望传说中的-USA和X轴作为列的名称[2001,2002 ... 2010],并且可以以更好的方式完成,而不需要像我一样经历单独的行。 `
答案 0 :(得分:6)
加载df时,需要指定“国家/地区名称”是您的索引。此外,在我看来,为了您的目的,使用国家作为列和年份行将是一个更明智的选择。
df = pd.read_csv(yourcsv, index_col='Name of Countries') #set column as index
df = df.T #Transpose df, now countries are your columns and years your rows
一旦你以这种方式加载df,一切都非常简单:
df.USA.plot(legend=True) #plot usa column
plt.show()