【发布时间】:2019-07-24 02:53:20
【问题描述】:
所以我有列表的数据框。
这是数据框的代码:
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_LIST 有1000 元素,而其他只有755.
但我需要的是用列表创建csv 文件的方法无论它们的长度如何。
有没有其他方法可以用列表创建CSV 文件?
或者无论如何忽略ValueError并仍然创建pandas dataframe?
【问题讨论】:
-
所以您想将
URL_LIST保留到最后并用nan 填充其余列? -
@Chris 没关系。无论每个列表的长度如何,任何创建 CSV 文件的方法对我来说都可以。
标签: python python-3.x pandas list