You could divide the rows into groups and pivot the groups into columns.
Short Version
number_rows = 2
df['cols'] = np.ceil(df['Column A'].expanding().count()/number_rows)
df.index = pd.Series(range(len(df))) % number_rows
df = df.pivot(columns='cols', values='Column A')
cols 1.0 2.0 3.0 4.0
0 Cell 0 Cell 2 Cell 4 Cell 6
1 Cell 1 Cell 3 Cell 5 Cell 7
The code will also work if your number of rows to split by (number_rows
) are not a multiple of length of your DataFrame, and missing values (np.nan
) will be added.
Long Version with Explanation of Steps
Create demo data
import pandas as pd
import numpy as np
df = pd.DataFrame({'Column A': [f'Cell {i}' for i in range(4)]})
Column A
0 Cell 0
1 Cell 1
2 Cell 2
3 Cell 3
Create columns for later columns and rows
number_rows = 3 # 3 instead of 2 to illustrate filling with missing values
df['cols'] = np.ceil(df['Column A'].expanding().count()/number_rows)
df['cols'] = 'Column '+df['cols'].astype(int).astype(str)
df['rows'] = pd.Series(range(len(df))) % number_rows
Column A cols rows
0 Cell 0 Column 1 0
1 Cell 1 Column 1 1
2 Cell 2 Column 1 2
3 Cell 3 Column 2 0
Pivot table
df = df.pivot(columns='cols', index='rows', values='Column A')
cols Column 1 Column 2
rows
0 Cell 0 Cell 3
1 Cell 1 NaN
2 Cell 2 NaN
You can remove the column and index names with:
df.columns.name = df.index.name = None