Read multiple CSV files into separate DataFrames in Python
Sometimes you might need to read multiple CSV files into separate Pandas DataFrames. Importing CSV files into DataFrames helps you work on the data using Python functionalities for data analysis.
In this article, we will see how to read multiple CSV files into separate DataFrames. For reading only one CSV file, we can use pd.read_csv() function of Pandas. It takes a path as input and returns DataFrame.
df = pd.read_csv("file path")
data frame
Read a single CSV file into a DataFrame
Let’s look at the following example to see how to read a single CSV file into a DataFrame:
Example:
- Python3
Python3
# import module import pandas as pd # read dataset df = pd.read_csv( "./csv/crime.csv" ) |
Output:
Read Multiple Files into separate DataFrame
Let’s look at the following example to see how to read multiple files into separate DataFrame in Python:
Example:
- Python3
Python3
# import module import pandas as pd # assign dataset names list_of_names = [ 'crime' , 'username' ] # create empty list dataframes_list = [] # append datasets into the list for i in range ( len (list_of_names)): temp_df = pd.read_csv( "./csv/" + list_of_names[i] + ".csv" ) dataframes_list.append(temp_df) |
Note: dataframes_list contains all the data frames separately.
dataframes_list[0]:
dataframes_list[1]:
Here is another approach, now suppose that there are many files, and we don’t know the names and numbers then, use the below code
- Python3
Python3
# import modules import os import pandas as pd # assign path path, dirs, files = next (os.walk( "./csv/" )) file_count = len (files) # create empty list dataframes_list = [] # append datasets to the list for i in range (file_count): temp_df = pd.read_csv( "./csv/" + files[i]) dataframes_list.append(temp_df) # display datasets for dataset in dataframes_list: display(dataset) |
Output:
Conclusion
Converting CSV files into Pandas DataFrame is very useful in data analysis, as DataFrame allows you to perform Python functionalities on the data. It allows you to perform operations like data cleaning, data filtration, etc.
In this tutorial, we have covered the methods to read CSV files into DataFrame in Python. We can read a single CSV file into a DataFrame and also read multiple CSV files into separate DataFrames.