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

Answer by Vaishali for Pandas groupby two columns and plot

$
0
0

IIUC,

df.groupby(['category','sex']).B.count().unstack().reset_index()\.plot.bar(x = 'category', y = ['f', 'm'])

enter image description here

Edit: If you have multiple columns, you can use groupby, count and droplevel.

new_df = df.groupby(['category','sex']).count().unstack()new_df.columns = new_df.columns.droplevel()new_df.reset_index().plot.bar()

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>