Import Multiple CSV Files into Pandas DataFrame

In this tutorial, we will learn how to import multiple CSV files (or all CSV files from the specified folder) into Pandas DataFrame? By Pranit Sharma Last updated : April 18, 2023

Steps to Import Multiple CSV files into Pandas DataFrame

The following are the steps to import multiple CSV files into Pandas DataFrame:

  • Read the CSV files using the read_csv() method.
  • Then, use the pandas.concate() method to merge them, it appends the each given DataFrame at the end of another to create a single DataFrame.

Example

# Importing pandas package
import pandas as pd

# Read CSV files from List
df = pd.concat(map(pd.read_csv, ['mycsv.csv', 'mycsv1.csv']))

# print the dataframe
print(df)

Using glob.glob() method

To import multiple CSV files (or all CSV files from the specified folder) into Pandas DataFrame, you can use glob.glob() method which takes the path of the folder where all the required files are located. Secondly, it takes the string as a parameter which works as an identification of the required file.

Let us understand with the help of an example.

Example

# Importing pandas package
import pandas as pd

# Importing glob library in order to get
# a list of all the required files
import glob

# Requesting a list of all the csv files present
# in a specified folder
req_files = glob.glob("C:/Users/hp/Desktop/Includehelp/*.csv")

# Importing multiple CSV files and
# concatenating them
data = pd.concat(map(pd.read_csv, req_files,),ignore_index=True)

# Creating the dataframe for
# the combined data
df3=pd.DataFrame(data)

# Printing the combined DataFrame
print("Combined Dataframe:\n",df3)

Output

Import multiple csv files

Python Pandas Programs »

All Python Pandas Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.