🔹 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
SOCIAL SHARE CARD GENERATOR