Read multiple CSV files into separate DataFrames in Python

Configurare noua (How To)

Situatie

We will see how to read multiple CSV files into separate DataFrames. For reading only one data frame we can use pd.read_csv()function of pandas. It takes a path as input and returns data frame like df = pd.read_csv(“file path”).

Solutie

Pasi de urmat

# import module
import pandas as pd

# read dataset
df = pd.read_csv(“./csv/crime.csv”)

 

Here, crime.csv is the file in the current folder. CSV is the folder that contains the crime.csv file and CSV Reader.ipynb is the file containing the above code.

It is the data frame that is read from the above function. One more file is present in the folder named – username.csv. To read them both and store them in different data frames use the below code

# 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)

dataframes_list contains all the data frames separately

dataframes_list[1]:

Tip solutie

Permanent

Voteaza

(5 din 15 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?