How to write to csv a dataframe with config line before headers
Question:
I’m using a pyhton script to manage different logfiles from an acquisition system, boards ecc.
I’m building a big dataframe which is the merge of other dfs and at the end i’m putting it to a csv file, below last line:
dF_COMP.to_csv(path1 + nomefile, sep=';', decimal= ',' , columns=columns)
I want to add in the first line of the csv a row with configuration parameters.
I can build the csv with the dataframe, then open it again and then add the line but I think is not the best idea as I expect to build even big files…
Any better idea?
Answers:
I can build the csv with the dataframe, then open it again and then add the line but I think is not the best idea as I expect to build even big files…
Do the opposite:
with open(path1 + nomefile, 'w') as csvfile:
csvfile.write(f'# Config blah blahn') # note the '#' as comment character
df.to_csv(csvfile, sep=';', decimal= ',', columns=columns)
To read the file again with pd.read_csv
:
pd.read_csv(path1 + nomefile, comment='#') # to skip lines begin with '#'
# OR
pd.read_csv(path1 + nomefile, skiprows=1) # to skip the first line
I’m using a pyhton script to manage different logfiles from an acquisition system, boards ecc.
I’m building a big dataframe which is the merge of other dfs and at the end i’m putting it to a csv file, below last line:
dF_COMP.to_csv(path1 + nomefile, sep=';', decimal= ',' , columns=columns)
I want to add in the first line of the csv a row with configuration parameters.
I can build the csv with the dataframe, then open it again and then add the line but I think is not the best idea as I expect to build even big files…
Any better idea?
I can build the csv with the dataframe, then open it again and then add the line but I think is not the best idea as I expect to build even big files…
Do the opposite:
with open(path1 + nomefile, 'w') as csvfile:
csvfile.write(f'# Config blah blahn') # note the '#' as comment character
df.to_csv(csvfile, sep=';', decimal= ',', columns=columns)
To read the file again with pd.read_csv
:
pd.read_csv(path1 + nomefile, comment='#') # to skip lines begin with '#'
# OR
pd.read_csv(path1 + nomefile, skiprows=1) # to skip the first line