簡體   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?

我有兩個清單。

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

list2 = [111, 222, 333]

有時 list1 的值比 list2 的多,有時 list2 的值比 list1 的多。

我怎么能像這樣在 CSV 文件上寫字。

abc 111

bcd 222

efg 333

ijk

lpo

在此處輸入圖像描述

您可以嘗試使用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