I’m running a script every day to get the date and values that I save as a data frame. Something like this:
df.head(4) Date Value 11/02/2021 00:00:00 550 11/02/2021 01:00:00 582 11/02/2021 02:00:00 655 11/02/2021 03:00:00 773
If I use the command ‘df.to_csv(“file.csv”)’ I get my data frame in an excel sheet. However, when I run the script for the following day (12/02/2021) I want to get the values for the same excel sheet. How can I do this?
If for day 12/02/2021 I use ‘df.to_csv(“file.csv”)’, it will eliminate my tables from the previous day and substitute them with the new values. I want to run the script every day and save it on the same sheet (all of the values). Is there any way to save the values always on the same column while keeping past values and adding new values?
Advertisement
Answer
You can use it directly without opening in python with df.to_csv(df, mode='a')
in append
mode. I wrote some little example
# EXAMPLE a = np.zeros((2,3)) b = np.ones((5,3)) df_a = pd.DataFrame(a, columns = ['a','b','c']) df_b = pd.DataFrame(b, columns = ['a','b','c']) df_a.to_csv('test.csv') df_b.to_csv('test.csv', mode='a', header = False) # mode = 'a', is the append mode, if you don't write it is 'w' - write, by default # header = False, in order not to repeat header in the middle