Quantcast
Channel: Pandas groupby two columns and plot - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Pandas groupby two columns and plot

$
0
0

I have a dataframe like this:

import numpy as npimport pandas as pdimport matplotlib.pyplot as plt%matplotlib inlinedf = pd.DataFrame({'category': list('XYZXY'), 'B': range(5,10),'sex': list('mfmff')})

I want to plot count of sex male or female based on category from column 'category'.

I tried:
df.groupby(['category','sex'])['category','sex'].count().plot.bar()

But this gives:
enter image description here

How do I get the count of sex per category?


Viewing all articles
Browse latest Browse all 4

Trending Articles