我有一个这样组织的大型数据框:
Timestamp slot_ID counter1 Counter2
1552227371 1 0 1
1552227372 2 1 0
1552227373 3 1 1
... ... .. ...
1552229621 100 1 1
1552229622 1 1 0
1552229623 2 0 1
1552229624 3 1 1
... ... .. ...
1552229626 100 1 1
我想基于slot_ID拆分数据帧,而不是为100个slot_ID计算不同计数器的总和。计数器1和计数器2始终具有值0或1。这意味着每100个插槽的总和小于或等于100。
def consecutive_groups(iterable, ordering=lambda x: x):
for k, g in groupby(enumerate(iterable), key=lambda x: x==100):
yield map(itemgetter(1), g)
grpsper100 = consecutive_groups(df_node['slot_ID'].index)
dfs = {i: df.iloc[list(j)] for i, j in enumerate(grpsper100, 1)}
for key, value in dfs.items():
#print(value['slot_ID'])
print(key)
print(value['Counter1'].sum())
print(value['Counter2'].sum())
但是我的代码没有给我预期的答案:
key: 1
Counter1: 119083
Counter2: 117400
请问如何更正我的代码?