Home > other >  How to concatenate two columns so that one comes below the other in pandas dataframe?
How to concatenate two columns so that one comes below the other in pandas dataframe?

Time:11-03

  Col-1    Col-2
0 Erin     Tanya
1 Cathy    Tom
2 Ross     Wes

This is my dataset

I need the result to look like this:

  New_column
0 Erin
1 Cathy
2 Ross
3 Tanya
4 Tom
5 Wes

I tried using .map, append, concat and .ravel but no luck. Any help would be appreciated :)

CodePudding user response:

With numpy.ravel, you can use:

out = pd.DataFrame({'New_column': df.to_numpy().ravel(order='F')})

# or
out = pd.DataFrame({'New_column': np.ravel(df, order='F')})

output:

  New_column
0       Erin
1      Cathy
2       Ross
3      Tanya
4        Tom
5        Wes

CodePudding user response:

Use DataFrame.melt:

df1 = df[['Col-1','Col-2']].melt(value_name='New_column')[['New_column']]

print (df1)
  New_column
0       Erin
1      Cathy
2       Ross
3      Tanya
4        Tom
5        Wes
  • Related