简体   繁体   中英

Count the occurance of unique values based on two columns

I'm trying to count the number of times a number (Knumber) occurs for each of the categories (category), below is my sample data.

Knumber category
K9  red
K1  white
K1  white
K9  white
K6  blue

I'm attempting make it into the following using pandas.

Knumber category    count
K9  red 1
K1  white   2
K9  white   1
K6  blue    1

I've fiddled around with value.counts using df['Knumber'].value_counts() but obviously that only counts Knumbers, can you please help me bring my other column 'category' into the equation?

Use Pandas groupby and the size function to get the count. The agg method allows us to pass a name for the aggregated column.

 (df
.groupby(['Knumber','category'])
.agg(count= ('category','size'))
.reset_index()
 )


   Knumber  category    count
0   K1  white   2
1   K6  blue    1
2   K9  red 1
3   K9  white   1

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