Here’s a way to combine multiple CSV files using Pandas. By merging two CSV files, you can create a new one.

ON THIS PAGE

  1. CSV File-1
  2. CSV FIle-2
  3. Pandas Logic to Combine CSV files
  4. The Final Result: Merged(or) Combined CSV
Combine CSV files
Photo by Pixabay on Pexels.com

CSV File-1

s1.csv
id	title	           fname lname
1000	Developer		
2000	Project Lead		
3000	Dev Manager		
4000	Senior Dev Manager

CSV FIle-2

s2.csv
 id,fname,lname
1000,John,Smith
2000,Jane,Stone
3000,Dave,Dodds
4000,Jack,Jones

Pandas Logic to Combine CSV files

The requirement is for the NULL values of the s1.csv file to be replaced with the s2.csv values. Here is the Pandas’ logic.

import pandas as pd

df1 = pd.read_csv("s1.csv")
df2 = pd.read_csv("s2.csv")
df1["fname"] = df2["fname"]
df1["lname"] = df2["lname"]

df1.to_csv('user_merged.csv', index=False)

The Final Result: Merged(or) Combined CSV

The final output file after merging the data from the s2.csv file.

id,title,fname,lname
1000,Developer,John,Smith
2000,Project Lead,Jane,Stone
3000,Dev Manager,Dave,Dodds
4000,Senior Dev Manager,Jack,Jones

This way you can merge the data from multiple files to a new file.

Related