DEV Community

Dolly Sharma
Dolly Sharma

Posted on

Three different ways to combine data in Pandas β€” concat, merge, and join.

πŸ”Ή 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)
Enter fullscreen mode Exit fullscreen mode
  • Stacks df2 below df1
  • Columns should be same (ideally)

🧠 Think: β€œappend rows”


πŸ“Œ Column-wise (axis=1)

combined = pd.concat([df1, df2], axis=1)
Enter fullscreen mode Exit fullscreen mode
  • 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")
Enter fullscreen mode Exit fullscreen mode
  • Only keeps matching values

πŸ“Œ Left join

merged = pd.merge(df1, df2, how="left", on="common_column")
Enter fullscreen mode Exit fullscreen mode
  • Keeps all rows of df1
  • Matches from df2 (NaN if no match)

πŸ“Œ Inner join

merged = pd.merge(df1, df2, how="inner", on="common_column")
Enter fullscreen mode Exit fullscreen mode
  • Same as default
  • Only common rows

🧠 Think: β€œSQL JOIN using a column”


πŸ”Ή 3. df.join() β†’ Index-based join

joined = df1.join(df2, how="inner")
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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)