如何在pandas DataFrame中对连续值进行分组 [英] How to groupby consecutive values in pandas DataFrame
本文介绍了如何在pandas DataFrame中对连续值进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在DataFrame中有一列,其值是:
I have a column in a DataFrame with values:
[1, 1, -1, 1, -1, -1]
我该如何将它们分组?
[1,1] [-1] [1] [-1, -1]
推荐答案
for i, g in df.groupby([(df.a != df.a.shift()).cumsum()]):
print (i)
print (g)
print (g.a.tolist())
a
0 1
1 1
[1, 1]
2
a
2 -1
[-1]
3
a
3 1
[1]
4
a
4 -1
5 -1
[-1, -1]
这篇关于如何在pandas DataFrame中对连续值进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文