Mimikyu o_0
Mimikyu o_0

Reputation: 57

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

  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 :)

Upvotes: 0

Views: 259

Answers (2)

jezrael
jezrael

Reputation: 862541

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

Upvotes: 1

mozway
mozway

Reputation: 260455

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

Upvotes: 1

Related Questions