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:
I think that you have assumed that the number of observations within each consecutive group will be the same. My approach is:
Prepare the data:
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] )
And the solution:
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')
Which returns:
Group 1.0 2.0
Observations
1.0 19218.965703 19279.216956
2.0 19247.621650 19330.087371
3.0 19232.651322 19304.316973