Home > Back-end >  How to copy a whole column to the end of another?
How to copy a whole column to the end of another?

Time:06-26

This might be an easy question, but I can't find the solution, I've searched in google but I only found the way to copy it to a new column, but not to an existing one. Basically, if I have this two columns:

Pilot1 Pilot2
Kevin Tom
Russell Richard
Max

I just want to know how to move the whole column Pilot2 to the first one, and then delete it:

Pilot1
Kevin
Russell
Max
Tom
Richard

CodePudding user response:

You can try this -

pd.concat([df["Pilot1"], df["Pilot2"]]).reset_index(drop = True)

Output -

0      Kevin
1    Russell
2        Max
3        Tom
4    Richard
5        NaN
dtype: object

CodePudding user response:

You can melt:

(df
 .melt(value_name='Pilot1')
 .drop('variable', axis=1)
 .dropna()
 )

Output:

    Pilot1
0    Kevin
1  Russell
2      Max
3      Tom
4  Richard
  • Related