I
I
irina_leifijtijhiodu2021-09-28 16:21:41
Python
irina_leifijtijhiodu, 2021-09-28 16:21:41

How else can you loop through CSV files using pandas and then concatenate them?

Hello.

I have 10 csv files named data1.csv , data2.csv , data3.csv .... data10.csv that need to be read through the pandas library and joined into one big dataframe.
How to do it with a loop?

I wrote the following:
IN
file_name = 'data{}.csv'
df_list = []
for i in range(1,11):
df_list.append(pd.read_csv('/content/drive/MyDrive/study/booking/' + file_name.format(i))

booking_df=pd.concat(df_list)
booking_df

OUT

The output is a SyntaxError, what could be the problem

?
For example, in the hints for the task, I am asked to make a list of the available files, but I don’t understand why it is needed at all, it is possible to do something using this list:

IN
import pandas as pd
import os as os
files = os.listdir(' /content/drive/MyDrive/study/booking') # get a list of our files and save it to a variable files

OUT
['data1.csv',
'data2.csv',
'data3.csv',
'data4.csv',
'data5.csv',
'data6.csv',
'data7.csv',
'data8.csv',
'data9.csv',
'data10.csv']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-09-28
@irina_leifijtijhiodu

Three brackets opened, two closed

df_list.append(pd.read_csv('/content/drive/MyDrive/для учебы/booking'+ file_name.format(i))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question