Python Pandas ValueError Arrays Must be All Same Length

Solution 1:

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 = df.transpose()

Explanation:

This creates the DataFrame as each key (e.g. 'Links') was a row and like this the missing values are actually missing columns which is no problem for pandas (only missing rows lead to ValueError during creation) After that you transpose the DataFrame (flip the axis) and make the rows to columns, which results the DataFrame you initially wanted.

Solution 2:

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.

Solution 3:

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

Solution 4:

Duplicate variable names caused this problem for me