Assuming "col" your column, use simple slicing:
cols = df.loc[df.index[::2], 'col'].to_numpy()
vals = df.loc[df.index[1::2], 'col'].to_numpy()
out = pd.DataFrame([vals], columns=cols)
Output:
column 1 column 2 column 3
0 value value value
duplicated values
Considering an alternative example:
col
0 column 1
1 value 1
2 column 2
3 value 2
4 column 2 # this name already exists
5 value 3
If you have duplicated column names, then a pivot
might be useful:
mask = np.arange(len(df))%2 == 0
out = (df[mask]
.assign(values=df['col'].shift(-1),
idx=lambda d: d.groupby('col').cumcount()
)
.pivot(index='idx', columns='col', values='values')
.rename_axis(index=None, columns=None)
)
Output:
column 1 column 2
0 value 1 value 2
1 NaN value 3
using numpy reshaping
If the column names are always in a logical order (1, 2, 3,…, 32, 1, 2, 3, …), then reshape
would be a good alternative:
df = pd.DataFrame({'col': [f'{c}{i%32+1}' if c=='column' else f'{c}{i+1}'
for i in range(50_000) for c in ['column', 'value']]})
N = 32
a = df['col'].to_numpy()
values = a[1::2]
out = pd.DataFrame(np.pad(values, (0, len(values)-len(values)//N*N),
constant_values=np.nan).reshape((-1, N)),
columns=a[:2*N:2])
NB. thank you @Nick for pointing that out.