I am trying to convert a wide dataframe [2r, 12c] to a long dataframe [4r,6c).
From this ...
0 1 2 3 4 5 6 7 8 9 10 11
0 1.0 0.9 0.8 0.5 0.4 0.3 0.7 0.9 0.2 0.1 0.4 0.3
1 0.6 0.8 0.5 0.3 0.7 0.1 0.4 0.2 0.8 0.6 0.4 0.1
... to this ...
0 1 2 3 4 5
0 1.0 0.9 0.8 0.5 0.4 0.3
1 0.7 0.9 0.2 0.1 0.4 0.3
2 0.6 0.8 0.5 0.3 0.7 0.1
3 0.4 0.2 0.8 0.6 0.4 0.1
My code is producing unexpected results:
0 1 2 3 4 5 6 7 8 9 10 11
0 1.0 0.9 0.8 0.5 0.4 0.3
1 Nan Nan Nan Nan Nan Nan 0.7 0.9 0.2 0.1 0.4 0.3
etc...
My code:
import pandas as pd
import numpy as np
df = pd.DataFrame(
np.random.randn(2, 12)
)
slice_sz = 6
x = df.columns
col_num = len(x)
col_iter = int(col_num / slice_sz)
y = df.index
row_num = len(y)
df_list = pd.DataFrame([])
for row in range(row_num):
# print('Processing row', row)
i = 0
j = slice_sz - 1
for col in range(col_iter):
df_sliced = df.iloc[row, i:j]
df_sliced = df_sliced.to_frame()
df_sliced = df_sliced.transpose()
df_list = df_list.append(df_sliced)
df_sliced = []
i += slice_sz
j += slice_sz
I know there must be a more elegant way to do this. Thanks for your help,