Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tour = Tour Name

Start = Available reservations at the start

End = Amount of reservations left

csv file columns:

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 this with this given data.

like image 494
yasuomain Avatar asked Nov 16 '17 21:11

yasuomain


2 Answers

IIUC, call set_index and plot.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 annotating the bars too, take a look at Annotate bars with values on Pandas bar plots.

like image 113
cs95 Avatar answered Oct 23 '22 10:10

cs95


You can also do this without set_index()

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

enter image description here

like image 2
Vaishali Avatar answered Oct 23 '22 10:10

Vaishali