遍历大量 .mp3 链接以获取元数据标签并将其保存到 Excel 文件。结果出现这个错误。我感谢任何帮助。谢谢。
#print is_connected();
# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Links' : lines ,'Titles' : titles , 'Singers': finalsingers , 'Albums':finalalbums , 'Years' : years})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(xlspath, engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
#df.to_excel(writer, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
Traceback (most recent call last):
File "mp.py", line 87, in <module>
df = pd.DataFrame({'Links' : lines ,'Titles' : titles , 'Singers': finalsingers , 'Albums':finalalbums , 'Years' : years})
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 266, in __init__
mgr = self._init_dict(data, index, columns, dtype=dtype)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 402, in _init_dict
return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 5409, in _arrays_to_mgr
index = extract_index(arrays)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 5457, in extract_index
raise ValueError('arrays must all be same length')
ValueError: arrays must all be same length
原文由 Blue Island 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以这样做以避免该错误
解释:
这会创建 DataFrame,因为每个键(例如
'Links'
)都是一行,这样缺失值实际上是缺失的列,这对熊猫来说没有问题(只有缺失的行导致ValueError
在创建)之后,您转置 DataFrame(翻转轴)并将行变为列,这会产生您最初想要的 DataFrame。