2 回答

TA贡献1788条经验 获得超4个赞
您必须创建一个辅助系列,例如:
s=df.index.to_series().diff().fillna(1).ne(1).cumsum()
print(s)
Index
0 0
1 0
2 0
58 1
59 1
60 1
92 2
93 2
然后您可以将每个组存储在字典中并调用字典的每个键来引用 df:
d={f'df_{i}':g for i,g in df.groupby(s)}
print(d['df_0'])
print('\n')
print(d['df_1'])
print('\n')
print(d['df_2'])
Time Data
Index
0 6:00 A
1 6:05 D
2 6:10 B
Time Data
Index
58 10:50 C
59 10:55 A
60 11:00 D
Time Data
Index
92 13:40 A
93 13:45 B
另一种使用方式more_itertools:
from more_itertools import consecutive_groups
indices=[[*i] for i in consecutive_groups(df.index)]
#[[0, 1, 2], [58, 59, 60], [92, 93]]
d2={f'df_{e}':df.loc[i] for e,i in enumerate(indices)}

TA贡献1865条经验 获得超7个赞
另一种方法(基于stackoverflow上的早期答案,现在找不到链接)
group=[]
for k,g in df.groupby(df['Index'] - np.arange(df.shape[0])):
group.append(g)
然后,您可以使用
group[0], group[1] or group[2]
添加回答
举报