I am trying to save the list Mean1
in vertical column format. I present the current and expected outputs.
Mean1= [25.482954545454543,
9.914772727272727,
5.369318181818182,
4.545454545454545,
4.318181818181818,
4.176136363636363,
4.119318181818182,
4.090909090909091,
4.0625,
4.0625]
with open('Mean.csv', 'w ') as f:
f.write(f"Mean1 = {str(Mean1)}\n")
The current output is
The expected output is
CodePudding user response:
Try this :
import csv
Mean1= [25.482954545454543,
9.914772727272727,
5.369318181818182,
4.545454545454545,
4.318181818181818,
4.176136363636363,
4.119318181818182,
4.090909090909091,
4.0625,
4.0625]
Mean1 = [[e] for e in Mean1]
with open(r'path_to_your_outputCsv.csv', 'w', newline="\n") as f:
wr = csv.writer(f)
wr.writerow(['Mean1'])
wr.writerows(Mean1)
Or, if you're fine with pandas, you can store the elements of a list (each in one separated row) in a .csv
by using pandas.DataFrame
and pandas.DataFrame.to_csv
combined.
#pip install pandas
import pandas as pd
Mean1= [25.482954545454543,
9.914772727272727,
5.369318181818182,
4.545454545454545,
4.318181818181818,
4.176136363636363,
4.119318181818182,
4.090909090909091,
4.0625,
4.0625]
pd.DataFrame(Mean1, columns=['Mean1']).to_csv(r'path_to_your_outputCsv.csv', index=False)