当前位置:  开发笔记 > 编程语言 > 正文

如何在数据框中绘制行

如何解决《如何在数据框中绘制行》经验,为你挑选了1个好方法。

我有看起来像这样的数据集`

"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],并且可以以更好的方式完成它,而不必像我正在做的那样遍历单个行。`



1> 小智..:

加载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()

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有