[英]Python Pandas ValueError Arrays Must be All Same Length
遍歷大量 .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
您可以這樣做以避免該錯誤
a = {'Links' : lines ,'Titles' : titles , 'Singers': finalsingers , 'Albums':finalalbums , 'Years' : years}
df = pd.DataFrame.from_dict(a, orient='index')
df = df.transpose()
它告訴您數組(行、標題、finalsingers 等)的長度不同。 您可以通過以下方式測試
print(len(lines), len(titles), len(finalsingers)) # Print all of them out here
這將向您顯示哪些數據格式不正確,然后您需要進行一些調查以了解糾正此錯誤的正確方法是什么。
您可以用空元素填充最短的列表:
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
重復的變量名給我造成了這個問題
我在將 JSON 文件讀取到 Pandas 框架時遇到了同樣的錯誤。 添加linesbool,默認False參數解決了這個問題。
StringData = StringIO(obj.get()['Body'].read().decode('utf-8'))
mydata = pdf.read_json(StringData, lines=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.