VOOZH about

URL: https://www.geeksforgeeks.org/pandas/pandas-combine-dataframe/

⇱ Pandas Combine Dataframe - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Pandas Combine Dataframe

Last Updated : 23 Jul, 2025

Combining DataFrames in Pandas is a fundamental operation that allows users to merge, concatenate, or join data from multiple sources into a single DataFrame.

This article explores the different techniques we can use to combine DataFrames in Pandas, focusing on concatenation, merging and joining.


Output
 name age_x age_y
0 Alice 25.0 26.0
1 Bob 30.0 NaN
2 Charlie NaN 35.0

Method 1: Concatenating DataFrames

concat() function in Pandas is used to combine multiple DataFrames along rows (vertically) or columns (horizontally). It's most useful when you have DataFrames that we want to stack.

  • Use pd.concat() to combine DataFrames vertically or horizontally.

We can refer this article for detailed explanation: How to concatenate two datframes

Method 2: Merging DataFrames

merge() function in Pandas is used to combine DataFrames based on a common column (similar to SQL JOINs). It’s helpful when we need to match rows from different DataFrames based on key columns.

  • Use merge() to combine DataFrames based on a shared column.

Method 3: Joining DataFrames

join() method is used to combine two DataFrames based on their index or on a key column. It is a convenient method when joining DataFrames with a shared index.

  • Use join() to combine DataFrames based on their index or a key column.

Method 4: Combining DataFrames Horizontally

We can also combine DataFrames horizontally (along columns) using concat(). This is useful when we want to join different features or attributes of the same observations.

  • Use pd.concat() with the axis=1 argument to combine DataFrames horizontally.

Output
 Name Age Gender Department
0 John 28 Male HR
1 Jane 34 Female IT
2 Alice 25 Female Finance

Method 5: Using combine_first() for Combining DataFrames

combine_first() function is used to combine two DataFrames where the values in the first DataFrame are retained unless there are missing values (NaN), in which case the corresponding values from the second DataFrame are used.

  • Use combine_first() to combine DataFrames.

Output
 Name Age Department
0 John 28.0 HR
1 Jane 34.0 IT
2 Alice 25.0 Finance

Related Articles:

Comment

Explore