Python: Combining DataFrames (append, concat and join)

Rahul S
2 min readSep 8

df1.append(df2) -

  • Add rows in df1 to the end of df2 (columns should be identical)
  • Use the append() method to stack one DataFrame (df1) on top of another DataFrame (df2).
  • The DataFrames should have identical column names.
  • Returns a new DataFrame with the combined rows.

Example:

import pandas as pd

data1 = {'A': [1, 2, 3], 'B': [4, 5, 6]}
data2 = {'A': [7, 8, 9], 'B': [10, 11, 12]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

combined_df = df1.append(df2)
# Resulting DataFrame (combined_df):
# A B
# 0 1 4
# 1 2 5
# 2 3 6
# 0 7 10
# 1 8 11
# 2 9 12

pd.concat([df1, df2], axis=1)

  • Add columns in df1 to the end of df2 (rows should be identical)
  • Use the concat() function to concatenate DataFrames (df1 and df2) horizontally along columns.
  • The DataFrames should have identical row indices.
  • Specify axis=1 to concatenate columns.

Example:

import pandas as pd

data1 = {'A': [1, 2, 3], 'B': [4, 5, 6]}
data2 = {'C': [7, 8, 9], 'D': [10, 11, 12]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
concatenated_df = pd.concat([df1, df2], axis=1)
# Resulting DataFrame (concatenated_df):
# A B C D
# 0 1 4 7 10
# 1 2 5 8 11
# 2 3 6 9 12

df1.join(df2, on='col1', how='inner')

  • SQL-style inner join based on 'col1'
  • Use the join() method to perform a SQL-style inner join between two DataFrames (df1 and df2) based on a common column ('col1').
  • You can specify different types of joins (inner, outer, left, right) using the how parameter.

Example:

import pandas as pd

data1 = {'ID': [1, 2, 3], 'Name': ['Alice', 'Bob', 'Charlie']}
data2 = {'ID': [2, 3, 4], 'Age': [25, 30, 28]}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
merged_df = df1.join(df2.set_index('ID'), on='ID', how='inner')
# Resulting DataFrame (merged_df):
# ID Name Age
# 1 2 Bob 25
# 2 3 Charlie 30

These examples demonstrate how to combine DataFrames in various ways, either by appending rows, concatenating columns, or performing SQL-style joins based on common columns.

Rahul S

LLM, NLP, Statistics, MLOps | Senior AI Consultant, StatusNeo | IIT Roorkee | Connect: [https://www.linkedin.com/in/rahultheogre/]

Recommended from Medium

Lists

See more recommendations