Joan Arau
Joan Arau

Reputation: 151

Pandas concat with different columns

I have two dfs that I want to concat

(sorry I don't know how to properly recreate a df here)

A  B
a1 b1
a2 b2
a3 b3

A  C
a1 c1
a4 c4

Result:

A  B C
a1 b1 c1
a2 b2 NaN
a3 b3 NaN
a4 NaN c4

I have tried:

merge = pd.concat([df1,df2],axis = 0,ignore_index= True)

but this seems to just append the second df to the first df

Thank you!

Upvotes: 1

Views: 64

Answers (1)

sophocles
sophocles

Reputation: 13821

I believe you need an outer join:

>>> pd.merge(df,df2,how='outer')

    A    B    C
0  a1   b1   c1
1  a2   b2  NaN
2  a3   b3  NaN
3  a4  NaN   c4

Upvotes: 2

Related Questions