πΉ 1. pd.concat() β Stack or attach data
π Used when you want to combine DataFrames along rows or columns
π Row-wise (axis=0)
combined = pd.concat([df1, df2], axis=0)
- Stacks df2 below df1
- Columns should be same (ideally)
π§ Think: βappend rowsβ
π Column-wise (axis=1)
combined = pd.concat([df1, df2], axis=1)
- Adds df2 as new columns
- Works based on index alignment
π§ Think: βside-by-sideβ
πΉ 2. pd.merge() β Database-style join
π Used when you want to combine based on a common column (key)
π Default (inner join)
merged = pd.merge(df1, df2, on="common_column")
- Only keeps matching values
π Left join
merged = pd.merge(df1, df2, how="left", on="common_column")
- Keeps all rows of df1
- Matches from df2 (NaN if no match)
π Inner join
merged = pd.merge(df1, df2, how="inner", on="common_column")
- Same as default
- Only common rows
π§ Think: βSQL JOIN using a columnβ
πΉ 3. df.join() β Index-based join
joined = df1.join(df2, how="inner")
π Combines using index (not columns)
- Faster for index-based operations
- Equivalent to merge but simpler syntax
π§ Think: βmerge on indexβ
π₯ Quick Difference Table
| Method | Based On | Use Case |
|---|---|---|
| concat | index/axis | stacking data |
| merge | column (key) | SQL-style joins |
| join | index | quick index-based combine |
β When to use what?
- Use
concatβ when data is already aligned - Use
mergeβ when you have a common column - Use
joinβ when index is important
Top comments (0)