【发布时间】:2021-04-01 09:11:30
【问题描述】:
我想每小时计算重复的行数。
我的数据框:
hour index name
08:00:00 1442 x
08:45:00 3434 y
08:30:00 1442 x
08:00:00 1442 x
08:45:00 3434 y
08:00:00 1442 x
我的代码:我尝试对每小时的数据进行分组并计数。转换没有帮助。
df_count= df.groupby('hour')[['index','name']].count()
这是错误:
TypeError: only integer scalar arrays can be converted to a scalar index
这是我想要的输出:
hour index name count
08:00:00 1442 x 3
08:30:00 1442 x 1
08:45:00 3434 y 2
【问题讨论】:
-
要获取每组的元素个数,只需使用 df.groupby("hour").size()
-
@tanglef 它不起作用。我犯了同样的错误。但我想要所有行上的不同,而不仅仅是小时。我在问题中添加了我正在寻找的他的输出。
-
好吧,那么对多列进行groupby,可以在groupby中给出列名的列表。
-
@tanglef 同样的错误——这是我试图运行的 df_count= df.groupby(['hour', 'index','name']).size()
-
您的列的 dtype 是什么?您可以粘贴代码来重建您的确切数据框吗?