Group by consecutive index numbers

前端 未结 6 2064
忘掉有多难
忘掉有多难 2021-01-03 19:00

I was wondering if there is a way to groupby consecutive index numbers and move the groups in different columns. Here is an example of the DataFrame I\'m using:



        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 19:37

    This is a groupby + pivot_table


    m = df.index.to_series().diff().ne(1).cumsum()
    
    (df.assign(key=df.groupby(m).cumcount())
        .pivot_table(index='key', columns=m, values=0))
    

                    1             2
    key
    0    19218.965703  19279.216956
    1    19247.621650  19330.087371
    2    19232.651322  19304.316973
    

提交回复
热议问题