简体   繁体   中英

Count number of rows per group in pandas datafreame

I have a data frame like so:

   canopy  speed
0       1    3.3
1       2    3.3
2       2    3.1
3       2    3.1
4       2    3.1
5       2    3.0
6       2    3.0
7       2    3.5
8       2    3.5

I want to count the number of rows (observations) for each combination of canopy and speed and plot it. I expect to see in the plot something like that:

canopy = 2:

3.3   1
3.1   3
3.0   2
3.5   3

You could do:

df.groupby('canopy')['speed'].value_counts().unstack('canopy').plot.bar()

This gives you some options, for example normalizing within each group (to get frequency instead of count):

(df
 .groupby('canopy')['speed']
 .value_counts(normalize=True)
 .unstack('canopy').plot.bar()
)

And, of course, you could control the rounding of the speed values (as @QuangHoang rightly mentioned: not a good idea to group on floats --to which I would add: without some rounding):

(df
 .assign(speed=df['speed'].round(0))
 .groupby('canopy')['speed']
 .value_counts()
 .unstack('canopy').plot.bar()
)

Try:

df.canopy.eq(2).groupby(df['speed']).sum()

Output:

speed
3.0    2
3.1    3
3.3    1
3.5    2
Name: canopy, dtype: int64

Note it's really not a good idea to group on floats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM