I am working on some data and I wanted to save it in a file with the extension .csv
. To do this, what I do is save these in a dictionary and then I would try to save them in the .csv
. I do the following,
###### DATA ######
atleta = 'Frink'
familia = None
subfamilia = None
fecha = '03/11/2020'
code = 'SP00003'
duracion = '00:35:00'
ejers = ['Flexiones rodillas','Sentadilla profunda','Fondos en banco','Zancadas','Dominada australiana prona','Rodillas a la barra']
series = [3,3,3,3,3,3]
reps = [1,25,10,15,1,5]
reps_estaticos = None
rest_series = 90
rest_reps = 90
mark_total = None
mark_ejers = [['MD','MD','MD'],['F','J','J'],['F','F','J'],['F','J','J'],['F','F','F'],['F','F','F']]
mark_app = None
coments = None
##################
myData = {
'Familia' : familia, 'Sub-Familia' : subfamilia, 'Fecha' : fecha, 'Atleta': atleta,
'Code': code, 'Duracion' : duracion, 'Ejercicios': ejers,
'Series' : series, 'Repeticiones' : reps, 'Repeticiones estaticos' : reps_estaticos,
'Descanso series' : rest_series, 'Descanso repeticiones' : rest_reps,
'Evaluacion gobal WOD' : mark_total, 'Evaluacion individual ejercicio' : mark_ejers, 'Evaluacion APP' : mark_app,
'Comentarios' : coments
}
# Finalmente guardamos la información en un fichero .csv
try: # Si existe fichero, añadimos los datos
df = read_csv(myPath+'data/data.csv')
df = df.append(myData, ignore_index=True)
except: # Si no existe, se crea.
df = DataFrame(myData)
df.to_csv(myPath+'data/data.csv', index=None, header=True)
As you can see, I make a differentiation between whether or not the file data.csv
in question exists, because if it does not exist, it would have to be created and if it does exist, the data would have to be added to the already existing file. Here comes the problem , and it is that depending on whether you perform one or the other action, the data is saved differently.
If the file is created, it generates line breaks to save the data. Specifically, these jumps are based on the content of the lists. Then you see how it is,
On the other hand, if the file already exists, it saves everything for me on the same line (which was initially my goal to simplify the file with the minimum number of rows). Below you can see how it looks.
That is, as a whole the table right now looks like the following image, when I just wanted two lines to input.
I understand that there is a parameter that I am not defining, I have played with the index
e ignore_index
but I do not get it. Why does this happen?
Thank you very much.
Continuing with your code I would make the following changes:
.append()
to the functionconcat()
Therefore I have changed the way in which the data is created and I have used
pd.concat()
Bonuses
I have added the pandas import with the alias
pandas as pd
. By convention this is usually the case.I don't know if this is your case, but there are people who in libraries such as
numpy
orpandas
import functions directly, instead of doing it with the alias convention because they think that it will be faster that way, or less resources will be used. *This is not true, and it only makes it difficult for other programmers or people on the team, so it is not recommendedNote: when I put "This is not true" it happens because of the way in which Python loads the libraries, a topic that I am not going to go into, since it is not the subject of this question.