Bob Hopez Bob Hopez - 1 year ago 40
Python Question

Merging two columns which don't overlap and create new columns

import pandas as pd

df1 = pd.DataFrame([["2014", "q2"]],
columns=('Year', 'Quarter'))

df2 = pd.DataFrame([["2016", "q1"],
["2015", "q1"]],
columns=('Year', 'Quarter'))

print(df1.merge(df2, on='Year', how='outer'))

Results in:

Year Quarter_x Quarter_y
0 2016 NaN q1
1 2015 NaN q1
2 2014 q2 NaN

What I would like to get is:

Year Quarter
0 2016 q1
1 2015 q1
2 2014 q2

Is there a simple way to do this with the merge() or some other function?

Answer Source

You can use:

df1.merge(df2, on=['Year', 'Quarter'], how='outer').dropna()

to get:

   Year Quarter
0  2014      q2
1  2016      q1
2  2015      q1

You can also look into pd.concat().