Home > other >  Bar chart of counts of 1/0 data by group
Bar chart of counts of 1/0 data by group

Time:07-08

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.

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

CodePudding user response:

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

  • Related