While concat() simply glues tables together, pd.merge() combines DataFrames based on matching values in common key columns—identically to an SQL JOIN or Excel XLOOKUP / VLOOKUP.
how='inner' - Default)Keeps only the rows where the join key exists in both tables (the intersection):
how='left')Keeps all rows from the left table. If no match exists in the right table, right-side columns are filled with NaN:
how='right')Keeps all rows from the right table. Customer 5 (who is not in the customer master table) is included:
how='outer')Keeps all rows from both tables, unioning unmatched records with NaN:
When the left table calls the key client_id and the right table calls it cust_number:
indicator=TrueTo audit which table contributed each record (essential for data reconciliation):
Output:
pd.merge(df1, df2, on='key')?indicator=True add to the merged DataFrame?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Concatenating DataFrames (pd.concat) | Reshaping Data: Melt, Stack & Unstack |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.