Python Pandas ValueError数组必须全部相同

时间:2022-10-27 21:25:17

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

2 个解决方案

#1


10  

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()

#2


3  

It's telling you that the arrays (lines, titles, finalsingers, etc...) are not of the same length. You can test this by

它告诉你阵列(线,标题,终结者等......)的长度不一样。你可以测试一下

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.

这将向您显示哪些数据格式不正确,然后您需要对正确的方法进行调查。

#1


10  

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()

#2


3  

It's telling you that the arrays (lines, titles, finalsingers, etc...) are not of the same length. You can test this by

它告诉你阵列(线,标题,终结者等......)的长度不一样。你可以测试一下

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.

这将向您显示哪些数据格式不正确,然后您需要对正确的方法进行调查。