Reputation: 25
As stated in title, I need to split a pandas dataframe with columns containing million of rows in 2 equal parts. Than I need to merge the second half as new columns.
Here a small sample:
AAA 125
BBB 467
XXX 321
YYY 568
Must be reshaped in:
AAA 125 XXX 321
BBB 467 YYY 568
I try with append and concat, but row don't merge and show NaN
Upvotes: 1
Views: 18
Reputation: 38415
One way is by horizontally stacking the underlying array
pd.DataFrame(np.hstack( (df.values[:len(df)//2], df.values[len(df)//2:])))
0 1 2 3
0 AAA 125 XXX 321
1 BBB 467 YYY 568
Option 2: Using reshape
pd.DataFrame(np.reshape(df.values, (df.shape[0]//2, df.shape[1] * 2)))
Upvotes: 2