Join pandas DataFrames in CSV Files in Python – Merge & Export (Example Code)

In this tutorial you’ll learn how to join pandas DataFrames in different CSV files in the Python programming language.

Example: Import, Join & Export Two pandas DataFrames in CSV Files

import pandas as pd             # Import pandas
df1 = pd.read_csv('df1.csv')    # Read both CSV files
df2 = pd.read_csv('df2.csv')
df_all = pd.merge(df1,          # Merge two pandas DataFrames
                  df2,
                  on = "id")
df_all.to_csv('df_all.csv')     # Write merged DataFrame to new CSV

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top