1

我有一个像这样组织的大型数据框:

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

请问如何更正我的代码?

4

0 回答 0