简体   繁体   English

How to Write in csv or Excel file using CSV module or Pandas by combining two lists into one In Python?

[英]How to Write in csv or Excel file using CSV module or Pandas by combining two lists into one In Python?

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.有时 list1 的值比 list2 的多,有时 list2 的值比 list1 的多。

How am I able to write on a CSV File Like This.我怎么能像这样在 CSV 文件上写字。

abc 111

bcd 222

efg 333

ijk

lpo

在此处输入图像描述

You can try with itertools zip_longest then to_csv您可以尝试使用itertools zip_longest然后to_csv

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM