-3

I have a list of lists of pure data like this

a=[[1,2,3],
   [4,5,6],
   [7,8,9]]

How can I write a into a CSV file with each list in a column like this?

1 4 7 
2 5 8
3 6 9
1
  • make a Transpose of your DataFrame a=[[1,2,3],[4,5,6],[7,8,9]]; df = pd.DataFrame(a).T; df.to_csv("output.csv",index=False,header=False) Commented Jan 29, 2019 at 8:32

2 Answers 2

2

Use:

import csv
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(list(zip(*l)))
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

import csv
with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, delimiter=' ')
    writer.writerows(a)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.