---
title: "How to Combine CSV Files in Pandas: Best Example"
description: "The post provides a method for merging CSV files using Pandas, specifically replacing NULL values in one file with corresponding values from another."
url: https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/
date: 2023-06-19
modified: 2026-10-05
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/06/pexels-photo-271795.jpeg?fit=1880%2C1058&ssl=1
categories: ["Python"]
type: post
lang: en
---

# How to Combine CSV Files in Pandas: Best Example

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](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/#csv-file-1)
2. [CSV FIle-2](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/#csv-file-2)
3. [Pandas Logic to Combine CSV files](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/#pandas-logic-to-combine-csv-files)
4. [The Final Result: Merged(or) Combined CSV](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/#the-final-result-merged-or-combined-csv)

![Combine CSV files](https://i0.wp.com/srinimf.com/wp-content/uploads/2023/06/pexels-photo-269399.jpeg?resize=1733%2C1300&ssl=1)*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

- [12 Essential pandas-Python Interview Questions](https://srinimf.com/2023/05/07/12-essential-pandas-python-interview-questions/)DateMay 7, 2023
- [Python CSV Files: Best Example to Read and Write](https://srinimf.com/2022/07/06/how-to-handle-csv-files-in-python/)DateJuly 6, 2022
- [Pandas Top Interview Questions: Citius Tech](https://srinimf.com/2024/02/24/pandas-top-interview-questions-citius-tech/)DateFebruary 24, 2024

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F06%2F19%2Fhow-to-merge-multiple-csv-files-top-pandas-program%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F06%2Fpexels-photo-271795.jpeg%3Ffit%3D1200%252C675%26ssl%3D1&description=How%20to%20Combine%20CSV%20Files%20in%20Pandas%3A%20Best%20Example)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/06/19/how-to-merge-multiple-csv-files-top-pandas-program/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Combine%20CSV%20Files%20in%20Pandas%3A%20Best%20Example&body=https%3A%2F%2Fsrinimf.com%2F2023%2F06%2F19%2Fhow-to-merge-multiple-csv-files-top-pandas-program%2F&share=email)
-

### Like this:

Like Loading…
