Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas, produce a multiple plot bar chart based on a specific column

I have a dataframe which has the following data:

Age A
5   True
5   True
10  False
15  False
15  True
20  False
25  True

How can I make a bar chart plot which has the values of the total number of rows in age, so for example 7 as the Y axis and the age as the X axis, with each Age having a true/false bar plot. I would like something like this, however instead of it saying gender F/M, it does True/False, for each age range.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can use DataFrame.value_counts() (pandas > 1.1.0) or SeriesGroupBy.value_counts() (pandas < 1.1.0) + DataFrame.plot

df.value_counts().unstack(fill_value=0).plot(kind='bar')
#df.groupby('A')['Age']\
#  .value_counts().unstack(fill_value=0, level=0).plot(kind='bar') # <1.1.0

enter image description here

Also we could use seaborn.countplot

import seaborn as sns
sns.countplot(data=df, x="Age", hue="A")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading