Home > Back-end >  How to Write in csv or Excel file using CSV module or Pandas by combining two lists into one In Pyth
How to Write in csv or Excel file using CSV module or Pandas by combining two lists into one In Pyth

Time:12-04

I've got two lists.

list1 = ['abc', 'bcd', 'efg', 'ijk', 'lpo']

list2 = [111, 222, 333]

Sometimes list1 is more values than list2, and other times list2 is more values than list1.

How am I able to write on a CSV File Like This.

abc 111

bcd 222

efg 333

ijk

lpo

enter image description here

CodePudding user response:

You can try with itertools zip_longest then to_csv

import itertools
pd.DataFrame(itertools.zip_longest(list1,list2)).to_csv('Your.csv',header=None, index=None)
  • Related