Michele Montaruli
Michele Montaruli

Reputation: 25

Split a large pandas column and affiancate as new column without considering index

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

Answers (1)

Vaishali
Vaishali

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

Related Questions