按连续索引号分组

时间:2019-08-29 15:46:59

标签: python pandas numpy group-by

我想知道是否有一种方法可以对连续的索引号进行分组并将组移动到不同的列中。这是我正在使用的DataFrame的示例:

                 0
0     19218.965703
1     19247.621650
2     19232.651322
9     19279.216956
10    19330.087371
11    19304.316973

我的想法是通过顺序索引号进行汇总,并得到如下所示的信息:

                 0             1
0     19218.965703  19279.216956    
1     19247.621650  19330.087371
2     19232.651322  19304.316973

我一直试图将数据分成3个块,然后再分组,但我一直在寻找更多可用于对顺序索引号进行分组和重新排列的东西。 谢谢!

6 个答案:

答案 0 :(得分:18)

这是一种方法:

from more_itertools import consecutive_groups
final=pd.concat([df.loc[i].reset_index(drop=True) 
                    for i in consecutive_groups(df.index)],axis=1)
final.columns=range(len(final.columns))
print(final)

              0             1
0  19218.965703  19279.216956
1  19247.621650  19330.087371
2  19232.651322  19304.316973

答案 1 :(得分:8)

这是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

答案 2 :(得分:7)

使用新的pandas.Series创建新的pandas.MultiIndex

a = pd.factorize(df.index - np.arange(len(df)))[0]
b = df.groupby(a).cumcount()

pd.Series(df['0'].to_numpy(), [b, a]).unstack()

              0             1
0  19218.965703  19279.216956
1  19247.621650  19330.087371
2  19232.651322  19304.316973

类似,但有更多的脾气

a = pd.factorize(df.index - np.arange(len(df)))[0]
b = df.groupby(a).cumcount()

c = np.empty((b.max() + 1, a.max() + 1), float)
c.fill(np.nan)
c[b, a] = np.ravel(df)
pd.DataFrame(c)

              0             1
0  19218.965703  19279.216956
1  19247.621650  19330.087371
2  19232.651322  19304.316973

答案 3 :(得分:6)

pandas groupby

的一种方式
s=df.index.to_series().diff().ne(1).cumsum()
pd.concat({x: y.reset_index(drop=True) for x, y in df['0'].groupby(s)}, axis=1)

Out[786]: 
              1             2
0  19218.965703  19279.216956
1  19247.621650  19330.087371
2  19232.651322  19304.316973

答案 4 :(得分:2)

我认为您已经假设每个连续组中的观察次数相同。我的方法是:

准备数据:

import pandas as pd
import numpy as np

df = pd.DataFrame(data ={'data':[19218.965703 ,19247.621650 ,19232.651322 ,19279.216956 ,19330.087371 ,19304.316973]}, index = [0,1,2,9,10,11] )

解决方法:

df['Group'] = (df.index.to_series()-np.arange(df.shape[0])).rank(method='dense')
df.reset_index(inplace=True)
df['Observations'] = df.groupby(['Group'])['index'].rank()
df.pivot(index='Observations',columns='Group', values='data')

哪个返回:

Group                  1.0           2.0
Observations                            
1.0           19218.965703  19279.216956
2.0           19247.621650  19330.087371
3.0           19232.651322  19304.316973

答案 5 :(得分:1)

我的方式:

df['groups']=list(df.reset_index()['index']-range(0,len(df)))
pd.concat([df[df['groups']==i][['0']].reset_index(drop=True) for i in df['groups'].unique()],axis=1)

              0             0
0  19218.965703  19279.216956
1  19247.621650  19330.087371
2  19232.651322  19304.316973