How to combine rows to change the shape of a pandas dataframe

Solution 1:

You can use the underlying numpy array and numpy's reshape method:

df2 = pd.DataFrame(df.to_numpy().reshape((len(df)//2, -1)))

output:

   0  1  2  3  4  5  6  7
0  a  b  c  d  e  f  g  h
1  i  j  k  l  m  n  o  p
alternatively using pandas:
pd.concat([df.iloc[::2].reset_index(drop=True),
           df.iloc[1::2].reset_index(drop=True)],
           axis=1)

but you will have duplicated column names:

   0  1  2  3  0  1  2  3
0  a  b  c  d  e  f  g  h
1  i  j  k  l  m  n  o  p