Reputation: 83
I have a list (named df_split) that stores 576 data frames, and each data frame has 50 rows. I want to iterate through each data frame and save it as a separate CSV file inside a folder. I tried the following code but it only saved the last data frame as a CSV file inside the location that I specified. In this case, I assume I should have also coded the file name for each data frame to be something like file1.csv, file2.csv, etc., but my skills aren't enough.
Can somebody kindly suggest some example solutions? Here is the code that I tried:
for i in df_split: i.to_csv('./file.csv')
Upvotes: 1
Views: 468
Reputation: 862406
Use enumerate
for counter for new file names:
for i, df in enumerate(df_split, 1):
df.to_csv(f'file_{i}.csv')
Upvotes: 1
Reputation: 4879
You could pass the i
into the filename as well -
for i in df_split:
i.to_csv(f'file_{i}.csv')
Upvotes: 1