Reputation: 11
I have a list of dataframes (n = 275). I'd like to save each one of them as a separate csv file in the same directory on my PC. I'd like to write a function to do that automaticaly. maybe someone could give an advice how can I do that?
Can anybody assist me in this:
dframes_list - list of dataframe names
df_00001 - dataframe name example that I have now and that I expect.
Thank you in advance.
Upvotes: 1
Views: 2403
Reputation: 793
I think the following code will do what you want. Putting it here for others who may want to create separate dataframes from a list of dataframes in python
This an update to the answer provided by @CrepeGoat
import os
folderpath = "your/folder/path-where-to-save-files"
csv = 'csv' # output file type
for i, df in enumerate(dflist, 1):
filename = "df_{}.{}".format(i, csv)
filepath = os.path.join(folderpath, filename)
df.to_csv(filepath)
Upvotes: 2
Reputation: 2515
(does not address OP; leaving here for historical purposes)
You can do something simple by looping over the list and calling the DataFrame.to_csv
method:
import os
folderpath = "your/folder/path"
for i, df in enumerate(dframes_list, 1):
filename = "df_{}".format(i)
filepath = os.path.join(folderpath, filename)
df.to_csv(filepath)
Upvotes: 3