I have written a code to print out a list of values. I want to write the data obtained in a csv file. My calculation gives me 5 columns with each column consisting of 5 data. When I print the data in csv file, instead of getting five data in each row for a specific column I get one cell filled with all the values. The output looks like this:
I have written the code as:
import csv
with open("/home/Documents/test.csv", "a") as fp:
wr = csv.writer(fp, dialect='excel')
wr.writerow(list)
The result that I am getting in the output is :
The expected outcome is for column "A" the values should be written in each cell i.e. 0.002 should go in cell A1, 0.001905 should go in cell A2 and so on. Similarly for column B the value 0.001814 should go in cell B2 and so on. Being a newbie to python, I don't know if there is a better way to do it or optimise it. Any help would be much appreciated.
CodePudding user response:
If you use Pandas, try pd.concat
:
pd.concat(your_list, axis=1).to_csv('file.csv')
CodePudding user response:
This looks like you have a list of numpy arrays or pandas series? just based on your print out, the appearance of "dtype" does not indicate a pure python list.
It does look like some sort of list of iterables so you could try:
import csv
with open("/home/Documents/test.csv", "a") as fp:
wr = csv.writer(fp, dialect='excel')
for row in list
wr.writerow(row)