Python Pandas ValueError 数组的长度必须相同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40442014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas ValueError Arrays Must be All Same Length
提问by Blue Island
Iterates over a big list of .mp3 links to get the metadata tags and save it to an Excel file. Results in this error. I appreciate any help. Thanks.
遍历大量 .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
回答by Vivek Srinivasan
you can do this to avoid that error
您可以这样做以避免该错误
a = {'Links' : lines ,'Titles' : titles , 'Singers': finalsingers , 'Albums':finalalbums , 'Years' : years}
df = pd.DataFrame.from_dict(a, orient='index')
df.transpose()
回答by kypalmer
It's telling you that the arrays (lines, titles, finalsingers, etc...) are not of the same length. You can test this by
它告诉您数组(行、标题、finalsingers 等)的长度不同。您可以通过以下方式测试
print(len(lines), len(titles), len(finalsingers)) # Print all of them out here
This will show you which data is malformed and then you'll need to do some investigating into what the right way to correct this is.
这将向您显示哪些数据格式不正确,然后您需要进行一些调查以了解纠正此错误的正确方法是什么。
回答by raffaem
You can pad the shortest lists with empty elements:
您可以用空元素填充最短的列表:
def pad_dict_list(dict_list, padel):
lmax = 0
for lname in dict_list.keys():
lmax = max(lmax, len(dict_list[lname]))
for lname in dict_list.keys():
ll = len(dict_list[lname])
if ll < lmax:
dict_list[lname] += [padel] * (lmax - ll)
return dict_list
回答by cubeloid
Duplicate variable names caused this problem for me
重复的变量名给我造成了这个问题