Home >Database >Mysql Tutorial >How to Perform a Pandas Left Outer Join Across Multiple DataFrames with Multiple Join Columns?

How to Perform a Pandas Left Outer Join Across Multiple DataFrames with Multiple Join Columns?

Barbara Streisand
Barbara StreisandOriginal
2025-01-04 00:51:441022browse

How to Perform a Pandas Left Outer Join Across Multiple DataFrames with Multiple Join Columns?

Pandas Left Outer Join on Multiple DataFrames with Multiple Columns

In Pandas, merging dataframes is a common task. This becomes more complex when performing joins involving multiple tables and multiple join columns. For a left outer join, we preserve all rows from the left dataframe and fill in missing values from the right dataframe.

To merge dataframes df1, df2, and df3 with multiple join columns, we recommend a two-step approach:

Step 1: Merge df1 and df2

s1 = pd.merge(df1, df2, how='left', on=['Year', 'Week', 'Colour'])

Step 2: Merge the result from Step 1 with df3

Since we don't need the Year column for the second merge, we can drop it from df3 before joining:

df3_dropped = df3.drop('Year', axis=1)
df = pd.merge(s1, df3_dropped, how='left', on=['Week', 'Colour'])

This gives us the desired output:

   Year Week Colour  Val1  Val2 Val3
0  2014    A    Red    50   NaN  NaN
1  2014    B    Red    60   NaN   60
2  2014    B  Black    70   100   10
3  2014    C    Red    10    20  NaN
4  2014    D  Green    20   NaN   20

The above is the detailed content of How to Perform a Pandas Left Outer Join Across Multiple DataFrames with Multiple Join Columns?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn