Fixing pandas ValueError: Length of values does not match length of index – Pandas DataFrame.unique()

pandas ValueError: Length of values does not match length of index – Pandas DataFrame.unique() is an error which occurs when you try to assign a list/array in numpy to a data frame while both have different lengths.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Exploring the pandas ValueError: Length of values does not match length of index – Pandas DataFrame.unique()

This is an error which occurs when you try to assign a list/array in numpy to a data frame while both have different lengths.

The error usually looks like the error bellow. Make sure yours is similar.

                                                                       #
ValueError: Length of values does not match length of index
                                                                       #

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution : convert the array to a pandas Series using pd.Series( ).

The problem here is that we are trying to assign a list of numpy array to a data frame while they both have different lengths.

For example, if you take a dataframe of six rows , df = pd.DataFrame({‘X1’: [a,2,d,f,5,g]}) and assign a an array of five elements to it like in the code bellow. You will end up with the same error we have now.

                                                                       #
df['X2'] = [p,1,s,h]
                                                                       #

Now trying to assign a list/array of two elements to it:

The best solution to fix the problem is to convert the array to a pandas Series using pd.Series( ).

For the example above

                                                                       #
# instead of using
df['X2'] = [p,1,s,h]
# we can use the following pandas Series
df['X2'] = pd.Series([p,1,s,h])
                                                                       #

An other addition is to use reset_index( ) in order to reset the index.

Summing-up

This is the end of the article, the error could be confusing but with a little bit of investigating the error could be solved, thanks for reading our post and good luck with the scripts to come.

If you want to support us consider donating to our Kofi account using the red button on top of this page. Keep coding, keep learning Python and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/