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