简体   繁体   English

如何获取 python 中某一列的每个类别的计数?

[英]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.

相关问题 如何计算python中每列中缺失的数据? - How to count missing data in each column in python? 如何在 Python 中显示每列的最高计数 - How to display top count for each column in Python 如何从 Python 数据框中的每个类别中获取前 n 条记录? - How to get top n records from each category in a Python dataframe? 如何在 python/Padas 中获取二维 df 中每一列中连续正数的计数 - How can I get the count of consecutive positive number in each column in 2 dimensional df in python/ Padas 对于 Python 的 plotnine,如何通过每个方面的总计数获得计数百分比 - for Python's plotnine, how to get % of count by total count in each facet 如何计算列中每个值的百分比遵循 python pandas dataframe 中的每个类别 - How to calculate the percentage of each value in a column follow each category in python pandas dataframe 每个类别的分数计数 - count of the fraction of each category 如何使用python计算一列中每一行的唯一值? - How to count the unique values of each row in one column with python? 如何使用python计算每列excel中填充单元格的数量 - how to count number of filled cells in each column of excel using python 在 Python 中 - 如何计算“内容”列的每一行中“价格”一词的出现次数? - In Python - How to count the occurrences of the word “price” in each row of the column “content”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM