pandas 如何将此 csv 数据转换为条形图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47339352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:46:46  来源:igfitidea点击:

How do I convert this csv data into a bar chart?

pythonpandasplot

提问by yasuomain

Tour = Tour Name

旅游 = 旅游名称

Start = Available reservations at the start

开始 = 开始时的可用预订

End = Amount of reservations left

结束 = 剩余预订量

csv file columns:

.csv 文件列:

ID     |   Tour   | Start | End

12345  |  Italy   |  100  | 80

13579  |  China   |  50   | 30

24680  |  France  |  50   | 30

I have this so far

到目前为止我有这个

import pandas as pd

df = pd.read_csv("items4.csv",sep=",").set_index('ID')

d = dict(zip(df.index,df.values.tolist()))

print(d)
{12345: ['Italy', 100, 80], 13579: ['China', 50, 30], 24680: ['France', 50, 30]} #This is the output

I want to make a bar chart that looks something like thiswith this given data.

我想用给定的数据制作一个看起来像这样的条形图。

采纳答案by cs95

IIUC, call set_indexand plot.bar:

IIUC,调用set_indexplot.bar

df

      ID    Tour  Start  End
0  12345   Italy    100   80
1  13579   China     50   30
2  24680  France     50   30

df.set_index('Tour')[['Start', 'End']].plot.bar()
plt.show()


enter image description here

在此处输入图片说明

If you're interested in annotatingthe bars too, take a look at Annotate bars with values on Pandas bar plots.

如果您也对注释条形感兴趣,请查看在Pandas 条形图上使用值注释条形

回答by Vaishali

You can also do this without set_index()

您也可以在没有 set_index() 的情况下执行此操作

df.plot.bar(x = 'Tour', y = ['Start', 'End'])

enter image description here

在此处输入图片说明