Import multiple csv files and concat into one dataframe in pandas

Spread the love

In this post you will learn how to read several csv files from a directory into pandas and concatenate them into one big dataframe.

Here, I have some sales data inside the sales_data directory. And in each files we have 10 records.

Now, to concatenate all the sales data into a single dataframe, you need to write

import pandas as pd
from pathlib import Path

# please provide your own path
data_path = Path.cwd().joinpath("sales_data")

all_files = data_path.glob("*.csv")

df = pd.concat(pd.read_csv(file) for file in all_files)

You can see that all the sales data has been merged into one single dataframe.

Rating: 1 out of 5.

Leave a Reply