-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
Mehrdad Pedramfar
  • 9,313
  • 3
  • 31
  • 54
Bloodmoon
  • 1,186
  • 1
  • 17
  • 32
  • **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)` – Chanakya Jan 29 '19 at 08:32

2 Answers2

2

Use:

import csv
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(list(zip(*l)))
U11-Forward
  • 41,703
  • 9
  • 50
  • 73
1

Try this:

import csv
with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, delimiter=' ')
    writer.writerows(a)
Mehrdad Pedramfar
  • 9,313
  • 3
  • 31
  • 54