Python 将列表移动到csv的更好方法?

Python 将列表移动到csv的更好方法?,python,python-3.x,pandas,list,Python,Python 3.x,Pandas,List,所以我有列表的数据框架 以下是数据帧的代码: Cafe_dataframe = pd.DataFrame({'카페 URL' : URL_LIST, '카페명' : CafeTitle, '카테고리명' : Category_Name, '글쓴이ID' : NaverID,

所以我有列表的数据框架

以下是数据帧的代码:

Cafe_dataframe = pd.DataFrame({'카페 URL' : URL_LIST,
                            '카페명' : CafeTitle,
                            '카테고리명' : Category_Name,
                            '글쓴이ID' : NaverID,
                            '포스트 제목' : PostTitle,
                            '포스트일' : Posting_Date,
                            '조회수' : The_Number_of_Views,
                            '비디오수' : The_Number_of_Video,
                            '그림수' : The_Number_of_Picture,
                            '댓글수' : The_Number_of_Comment,
                            '글자수' : The_Number_of_Characters,
                            '키워드수' : The_Number_of_Keyword
                           })

Cafe_dataframe.to_csv('cafe_data.csv', index=True, encoding='UTF8')

path="./cafe_data.csv"
with open(path, 'r', encoding='UTF8', errors='replace') as infile, open('cafe_data_.csv', 'w', encoding='euc-kr', errors='replace') as outfile:
inputs = csv.reader(infile)
output = csv.writer(outfile)

for index, row in enumerate(inputs):
    output.writerow(row)

os.remove('cafe_data.csv')
这个错误被提出:

ValueError: arrays must all be same length
现在,我知道
dataframe
不能使用不同长度的列表,我检查了每个列表的长度,结果发现
URL\u list
1000
元素,而其他元素只有
755

但我需要的是一种方法来创建带有列表的
csv
文件,而不管列表的长度如何

是否有其他方法创建带有列表的
CSV
文件


还是可以忽略
ValueError
并仍然使用
集合创建
pandas数据帧

使用
集合。OrderedDict
itertools.zip

from collections import OrderedDict
from itertools import zip_longest

d = OrderedDict({"A": [0,1], "C": [0,1,2,4], "B": [0,1,2]})

df = pd.DataFrame(list(zip_longest(*d.values())), columns = d.keys())
print(df)
     A  C    B
0  0.0  0  0.0
1  1.0  1  1.0
2  NaN  2  2.0
3  NaN  4  NaN

注意:
OrderedDict
用于确保
d.values()
d.keys()
的顺序正确。如果您使用的是python 3.6或更高版本,则正常的
dict
就可以了。

因此您希望将
URL\u列表
保留到末尾,并用nan?@Chris填充其余列就可以了。任何创建CSV文件的方法,不管每个列表的长度如何,我都可以。一个简单的问题:它是否必须列出d=OrderedDict({“A”:[0,1],“C”:[0,1,2,4],“B”:[0,1,2]})中的所有元素?我可以简单地用列表的名称替换它吗?因为您已经有了一个
dict
,只需将它包装为
d=OrderedDict(您的dict是在\u pd.DataFrame内)
;)只是澄清一下,您试图转换的不是列表。它被称为列表的
dict
,其中
dict
的键是你的名字(即'카페 URL'),值是相应的列表。感觉你对术语有点困惑。