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:

YearQuarterValue02014      q22.012013      q11.022016      q13.032015      q13.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)YearQuarterValue02014      q22.012013      q11.022016      q13.032015      q13.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)YearQuarterValue02014      q2212013      q1122016      q1332015      q13

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"