I need to write to the same CSV every day, but every time I compile it rewrites the file from scratch. My idea would be to read the number of lines that the file has written and write the new information in that row.
I am currently doing it this way:
creacion = {0: [0.081818182000000003], 2: [0.30769230800000003], 8: [0.0]})
my_df = pd.DataFrame(creacion)
my_df.to_csv('out.csv', index=False, header=False,sep=';',decimal=',')
The output I get from the CSV is:
0,081818182;0,307692308;0
But if I compile again other information rewrites it from scratch:
creacion = {3: [0.0], 4: [0.22222], 5: [0.1]})
my_df = pd.DataFrame(creacion)
my_df.to_csv('out.csv', index=False, header=False,sep=';',decimal=',')
The output I get from the CSV is:
0;0,22222;0,1
The acceptable output would be:
0,081818182;0,307692308;0
0;0,22222;0,1
The other thing, could you also add the dictionary key directly?
What I want to do ideally is this:
0;0,081818182
2;0,307692308
8;0
3;0
4;0,22222;0,1
5;0,1
The problem is that you are truncating the file each time. Therefore, what you should do is open the file previously in "append" mode instead of "write" (which truncates what was already there).
You can do this using the parameter
mode
and putting "a" so that it overwrites the "w" that it has by default:More information on the reference page
pandas.DataFrame.to_csv()
.The example you give is a bit confusing because you create dataframes with different columns and at the end you pivot it, but you can achieve it with:
with outlet
I'm trying to write to a csv file based on what I have here my_df=pd.DataFrame({'Shot':[3], 'Score':[Score]}) my_df.to_csv('Scores2.csv', mode= 'a', index=False, header=True,sep=';',decimal=',')
what I want is that the title is only written once since it is coming out repeatedly