1

I have this simple code which saves a pandas dataframe to a csv file. As of now, it works by overwriting the filename so each time I run it it just replaces the old file with a new one with the same name. Is it possible to save this dataframe but have it create new files sequentially, ie if there is already some file called "filename1" in the directory make the new one called "filename2" so the data from the original file is not just lost?

import pandas
datamatrix= [[1,2,3],[1,2,3],[1,2,3]])
x=pandas.DataFrame(datamatrix)  
pandas.DataFrame.to_csv(x,"filename.csv",',')
4
  • 1
    Have a look at this question asked here already stackoverflow.com/questions/13852700/… Commented Jun 15, 2018 at 20:07
  • This is not a pandas specific question. You want to read the files in folder before save a new one Commented Jun 15, 2018 at 20:08
  • 1
    Possible duplicate of python: Create file but if name exists add number Commented Jun 15, 2018 at 20:09
  • You can explicitly check if a file exists before writing by using os.path.exists(filename), but enumerating it into infinity may not really what you want, it may be a better idea to add a timestamp in one way or another to it that conflicts only once. Something like filename_201806150311.csv Commented Jun 15, 2018 at 20:12

3 Answers 3

2

I would probably save it with a different timestamp, unless there is a specific reason to have numerical numbering.

Using time stamp

import pandas
import time
datamatrix= [[1,2,3],[1,2,3],[1,2,3]]
x=pandas.DataFrame(datamatrix)  
pandas.DataFrame.to_csv(x,"filename_" + time.strftime('%Y-%m-%d %H-%S') + ".csv",',')

You could also just do a unix time stamp

pandas.DataFrame.to_csv(x,"filename_" + str(int(time.time())) + ".csv",',')

How to get current time in python and break up into year, month, day, hour, minute?

What is the easiest way to get current GMT time in Unix timestamp format?

Sign up to request clarification or add additional context in comments.

Comments

0

As some of the comments have mentioned, this is not a question that has a pandas-specific answer. Simply use os.listdir(os.getcwd()) to list all of the files in your current working directory that you want to save the file to. If the filename that you are trying to save as already exists in the list returned from the above command, then increment your version number by 1, or save as a different filename entirely, up to you.

Comments

0

by using time.strftime and combinding f-string and raw string literals you can attach timestamp to your desired format

import pandas
import time
timestr = time.strftime("%Y%m%d-%H%M%S")
datamatrix= [[1,2,3],[1,2,3],[1,2,3]]
x=pandas.DataFrame(datamatrix)  
pandas.DataFrame.to_csv(fr'D:\filename_{timestr}.csv',encoding="utf-8", index=False, header=True)

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.