2016-11-05 99 views
3

對.mp3鏈接的大列表進行迭代以獲取元數據標籤並將其保存到Excel文件中。結果出現這個錯誤。我感謝任何幫助。謝謝。Python Pandas ValueError數組必須全部相同長度

#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 

回答

2

它告訴你數組(線,標題,finalsingers等)的長度不一樣。您可以通過

print(len(lines), len(titles), len(finalsingers)) # Print all of them out here 

測試這個這將顯示您的數據格式錯誤,那麼你就需要做一些調查到什麼來糾正這種正確的方式是。

6

你可以做到這一點,以避免錯誤

a = {'Links' : lines ,'Titles' : titles , 'Singers': finalsingers , 'Albums':finalalbums , 'Years' : years} 
df = pd.DataFrame.from_dict(a, orient='index') 
df.transpose() 
+0

這可能重複http://stackoverflow.com/questions/19736080/creating-dataframe-from-a-dictionary-where-entries-have-不同-長度 –

相關問題