簡體   English   中英

按連續索引號分組

[英]Group by consecutive index numbers

我想知道是否有一種方法可以對連續的索引號進行分組並將組移動到不同的列中。 這是我正在使用的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個塊,然后再進行groupby划分,但是我一直在尋找更多可用於對順序索引號進行分組和重新排列的東西。 謝謝!

這是一種方法:

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

這是一個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

使用新的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

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

我認為您已假設每個連續組中的觀察次數相同。 我的方法是:

准備數據:

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

我的方式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM