-1

I need to iterate through all files in a folder to open all csv files. After I got all the file directory in a list: a. I need to read them and append them to a dataframe with the same column names.

a = [] #empty list
for root, dirs, files in os.walk('C:\\Users\\thaiq\\Week5'):
    for file in files:
        if file.endswith('.csv') and 'Rentals' in file:
            a.append(str(os.path.join(root,file)))

ridedata = pd.DataFrame([]) #empty dataframe

for rides in a:
    file = pd.read_csv(rides,header=0) #open each file in list a
    ridedata.append(file) 

ridedata return empty, plz help :(

1
  • Unlike python list.append, df.append actually returns a result that needs to be assigned. But aside from that, you should be using pd.concat instead. Commented Jun 18, 2019 at 15:46

1 Answer 1

0
ridedata = pd.concat([ridedata, file])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.