TheNone
TheNone

Reputation: 5802

Add data to a dataframe column from another dataframe with Pandas

I have two dataframes:

df1:

name surname
jane doe
john doe

df2

name
james

I want to add df2 to df1:

df_final

name    surname 
james   doe
jane    doe
john

with df1.add(df2) I haven't correct result. how can I add df2's data to first column of df1?

Upvotes: 0

Views: 54

Answers (1)

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

Use append to do that i.e

df2.append(df1)

Output :

  name surname
0  james     NaN
0   jane     doe
1   john     doe

For your expected output you can do

df2.append(df).apply(sorted,key=pd.isnull).fillna('')
  name surname
0  james     doe
0   jane     doe
1   john        

Upvotes: 1

Related Questions