Python: Combining DataFrames (append, concat and join)

Rahul S
2 min readSep 8, 2023

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 =…

--

--