[英]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.请添加您的 output 的示例。 Possibly this is what you want.可能这就是你想要的。
amount_per_day = df.groupby('Timestamp')['Type'].nunique()
result = df.merge(amount_per_day, on='Timestamp')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.