1

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.

6
  • dataframe names are in same pattern? Commented Apr 14, 2020 at 14:10
  • yes, they are.. Commented Apr 14, 2020 at 14:12
  • For almost everything CSV, you should use the standard CSV python module. docs.python.org/3/library/csv.html. I suggest that you start there. Don't try and re-invent the wheel Commented Apr 14, 2020 at 14:13
  • Does this answer your question? Writing a pandas DataFrame to CSV file Commented Apr 14, 2020 at 14:18
  • No, because they are dicussing a single dataframe and I need to save a list of dataframes to separate csv files. Commented Apr 14, 2020 at 14:22

2 Answers 2

3

(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)
Sign up to request clarification or add additional context in comments.

6 Comments

That doesn't work ((( : AttributeError: 'str' object has no attribute 'to_csv'
AH! sorry, I misread the question; you have a list of dataframe names. are these filenames?
yes, they are. I've splited data and made a list of all dataframes I actually have now. Now I would like to save each one of them separately.
so you have a list of filenames, and you want to copy those files from one location to another? Am I understanding this right? Because if that's all you want to do, then there's no reason to even use pandas; you can just stick with file manipulations that os and sys make available to you. If that's correct let me know and I'll write up another answer.
I have 1 big dataframe that was splited to 275 dataframes. They are not saved and are only in the memory of environment. I'd like to save them in my PC memory as separate csv files. I hope I've answered on your question more clearly.
|
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.