Bob Hopez
Bob Hopez

Reputation: 783

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?

Upvotes: 0

Views: 375

Answers (1)

Stefan
Stefan

Reputation: 42875

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().

Upvotes: 1

Related Questions