简体   繁体   中英

How to get the count of each category of a column in python?

I have a data frame with a categorical column with several levels. I would like to count how many different categories there are per each day.

Timestamp   Type   value1    value2
2020-08-31  Orange  3.4       5.7
2020-08-31. Red     7.9       4.5
2020-08-31  Purple  6.3       2.7
2020-09-01  Yellow  3.4       1.6

The values are the mean of these values per each day based on the type. Now I'd like to add some columns with the counting of each type per day.

Please add an example of your output. Possibly this is what you want.

amount_per_day = df.groupby('Timestamp')['Type'].nunique()
result = df.merge(amount_per_day, on='Timestamp')

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