How to groupby consecutive values in pandas DataFrame

前端 未结 2 790
星月不相逢
星月不相逢 2020-11-22 16:45

I have a column in a DataFrame with values:

[1, 1, -1, 1, -1, -1]

How can I group them like this?

[1,1] [-1] [1] [-1, -1]
<         


        
2条回答
  •  清歌不尽
    2020-11-22 17:20

    Using groupby from itertools data from Jez

    from itertools import groupby
    [ list(group) for key, group in groupby(df.a.values.tolist())]
    Out[361]: [[1, 1], [-1], [1], [-1, -1]]
    

提交回复
热议问题