Noob Programmer
Noob Programmer

Reputation: 752

Bar plot from dataframe

I have a data frame that looks something like this.

print (df)
   a      b
0  1   5896
1  1   4000
2  1  89647
3  2     54
4  2   3568
5  2  48761
6  3   5896
7  3   2800
8  3   5894

enter image description here

And I want to make a bar plot. That looks like this.

enter image description here

I tried with groupby.()but it only prints only one value of 1 one values of 2 etc...

a = df_result.groupby(['column1'])['column2'].mean()
a.plot.bar()
plt.show()

Would appreciate some guidance how to solve the problem, so I would have all of the values in a chart.

Upvotes: 1

Views: 120

Answers (1)

jezrael
jezrael

Reputation: 863501

I think need cumcount with set_index and unstack first for reshape data:

a = df.set_index(['a',df.groupby('a').cumcount()])['b'].unstack()
print (a)
      0     1      2
a                   
1  5896  4000  89647
2    54  3568  48761
3  5896  2800   5894

a.plot.bar()

graph

Upvotes: 3

Related Questions