I wish to do the following iteratively for a list of files of the form 'name.csv':
- Read the file in with pandas,
- Apply a function "datacropper" to the file
- Generate a .csv of the updated file using the pandas .to_csv command, with name of the form 'cut-name.csv'.
These steps all work separately outside of a for loop. However, when I iterate over the files, I get the error:
TypeError: 'builtin_function_or_method' object is not iterable
Below is my attempt. How can I bypass this error?
nameslist = ['pi89_1','pi89_ph7_1','pi89_ph7p48','pi89_ph6p49','pi89_ph7_2']
for i in range(len(nameslist)):
nameslist[i] = pd.read_csv("/path/to/file/" str(nameslist[i]) ".csv", index_col = 0)
nameslist[i] = datacropper(str(nameslist[i]), 400, 800)
nameslist[i].to_csv("/path/to/file/" 'cut-' str(nameslist[i]) ".csv")
CodePudding user response:
Something more like this. Note that it is not necessary to use str() on your file names. They are already strings.
nameslist = ['pi89_1','pi89_ph7_1','pi89_ph7p48','pi89_ph6p49','pi89_ph7_2']
for name in nameslist:
df = pd.read_csv("/path/to/file/" name ".csv", index_col = 0)
df = datacropper(df, 400, 800)
df.to_csv("/path/to/file/" 'cut-' name ".csv")
