Skip to content Skip to sidebar Skip to footer

Merging Two Or More Columns Which Don't Overlap

Follow up to this post: Merging two columns which don't overlap and create new columns import pandas as pd df1 = pd.DataFrame([['2014', 'q2', 2], ['2013', 'q1',

Solution 1:

Not sure what's happening here, but if I do

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

I get:

   Year Quarter  Value
0  2014      q2    2.0
1  2013      q1    1.0
2  2016      q1    3.0
3  2015      q1    3.0

You may want to take a look at the merge, join & concat docs.

The most 'intuitive' way for this is probably .append():

df1.append(df2)

   Year Quarter  Value
0  2014      q2    2.0
1  2013      q1    1.0
2  2016      q1    3.0
3  2015      q1    3.0

If you look into the source code, you'll find it calls concat behind the scenes.

Merge is useful and intended for cases where you have columns with overlapping values.


Solution 2:

pandas concat is much better suited for this.

pd.concat([df1, df2]).reset_index(drop=True)

   Year Quarter  Value
0  2014      q2      2
1  2013      q1      1
2  2016      q1      3
3  2015      q1      3

concat is intended to place one dataframe adjacent to another while keeping the index or columns aligned. In the default case, it keeps the columns aligned. Considering your example dataframes, the columns are aligned and your stated expected output shows df2 placed exactly after df1 where the columns are aligned. Every aspect of what you've asked for is exactly what concat was designed to provide. All I've done is point you to an appropriate function.


Solution 3:

You're looking for the append feature:

df_final = df1.append(df2)

Post a Comment for "Merging Two Or More Columns Which Don't Overlap"