Homesand
Homesand

Reputation: 423

How to plot graph in complicated pandas groupby?

I have a DataFrame about rows-transaction like:

 Month        Category       Quantity
 3            A              1 
 3            A              1
 3            B              1  
 4            A              1
 4            B              1
 4            B              1

I used groupby function to count the quantity of each category in each month

df.groupby(['Month','Category']['Quantity'].count()

Results:

Month   Category    
3       A           2
        B           1
4       A           1
        B           2 

I don't know how to plot quantity graph for category ( A & B should be represented by different lines), x-axis is the time, in terms of output of groupby function. Many thanks

Upvotes: 1

Views: 93

Answers (1)

cs95
cs95

Reputation: 402353

You'd want to unstack and then call pd.DataFrame.plot:

df.groupby(['Month','Category'])['Quantity'].count().unstack().plot()
plt.show()

You get a graph with two lines, one per Category.

Upvotes: 2

Related Questions