I have about 10 CSV files that I'd like to append into one file. My thought was to assign the file names to numbered data_files, and then append them in a while loop, but I'm having trouble updating the file to the next numbered date_file in my loop. I keep getting errors related to "data_file does not exist" and "cannot concatenate 'str' and 'int' objects". I'm not even sure if this is a realistic approach to my problem. Any help would be appreciated.
import pandas as pd
path = '//pathname'
data_file1= path + 'filename1.csv'
data_file2= path + 'filename2.csv'
data_file3= path + 'filename3.csv'
data_file4= path + 'filename4.csv'
data_file5= path + 'filename5.csv'
data_file6= path + 'filename6.csv'
data_file7= path + 'filename7.csv'
df = pd.read_csv(data_file1)
x = 2
while x < 8:
data_file = 'data file' + str(x)
tmdDF = pd.read_csv(data_file)
df = df.append(tmpDF)
x += x + 1
x += 1orx = x + 1. Your last line increments x by 2. And you are trying to open files named 'data file1' , 'data file2', etc, which is why it is failing. You can't assign variable names like that, you might want to use a dictionary. Of course, you also shouldn't repeat the same line seven times almost verbatim, you could use a loop for that.