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

Bar chart of counts of 1/0 data by group

I’m completely stumped and have no-one to ask.

I have a pandas dataframe similar to the following just much larger, what I would like to do is create a bar graph for each district that shows how many birds were present, i.e. how many ‘1’s there are in the column. The ‘1’s stand for ‘was present’, ‘0’ for ‘not present’.

groupby district gives me nonsense numbers.

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

I’m sure it is something completely obvious, but I’m drawing a blank.

district area year swallow sparrow kite finch
aaa a_1 2010 1 0 0 1
aaa a_1 2011 1 1 0 1
aaa a_1 2015 0 1 1 1
aaa a_2 2009 1 1 1 1
aaa a_2 2011 1 0 0 0
bbb b_1 2009 1 1 1 1
bbb b_1 2012 0 0 1 0

>Solution :

Not sure which exact graph you expect. Here are a few hints.

Per district/bird:

(df
 .drop(columns=['area', 'year'])
 .groupby('district').sum()
 .plot.bar()
)

enter image description here

Per district, all birds:

(df
 .drop(columns=['area', 'year'])
 .groupby('district').sum().sum(1)
 .plot.bar()
)

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