ValueError: Length of values does not match length of index

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. ValueError: Length of values does not match length of index
  2. Solve the error by using a Pandas Series
  3. Incorrectly creating a DataFrame or a Series

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

The Pandas "ValueError: Length of values (X) does not match length of index (Y)" occurs when you try to assign a column of a NumPy array of a different length to a DataFrame.

To solve the error, use a Series instead of a NumPy array.

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) # A B C # 0 1 4 7 # 1 2 5 8 # 2 3 6 9 print(df) df['D'] = [10, 11] # ⛔️ ValueError: Length of values (2) does not match length of index (3) print(df)

value error length of values does not match length of index

Each column has 3 rows in the example, but we tried adding a new column that only contains 2 elements.

The error is also caused if you try to assign a column that points to a NumPy array of a different length.

main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) # A B C # 0 1 4 7 # 1 2 5 8 # 2 3 6 9 print(df) df['D'] = np.array([10, 11]) # ⛔️ ValueError: Length of values (2) does not match length of index (3) print(df)

The NumPy array only has 2 elements but each column in the DataFrame has 3 rows, so it is expected that the NumPy array should also have 3 elements.

# Solve the error by using a Pandas Series

You can solve the error by using a pandas Series instead of a list or a NumPy array.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) df['D'] = pd.Series([10, 11]) # A B C D # 0 1 4 7 10.0 # 1 2 5 8 11.0 # 2 3 6 9 NaN print(df)

using pandas series instead of numpy array

The code for this article is available on GitHub

The pandas.Series class is used to create a one-dimensional ndarray with axis labels.

However, notice that we have a NaN value because the Series only has 2 elements whereas each column in the DataFrame has 3 rows.

If you need to replace the NaN values in the DataFrame with zeros, use the DataFrame.fillna() method.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) df['D'] = pd.Series([10, 11]) df = df.fillna(0) # A B C D # 0 1 4 7 10.0 # 1 2 5 8 11.0 # 2 3 6 9 0.0 print(df)

replace nan with zero in dataframe

The code for this article is available on GitHub

The DataFrame.fillna() method fills NA/NaN with the supplied value.

You can also pass the inplace argument when calling fillna() to avoid reassigning the variable.

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) df['D'] = pd.Series([10, 11]) df.fillna(0, inplace=True) # A B C D # 0 1 4 7 10.0 # 1 2 5 8 11.0 # 2 3 6 9 0.0 print(df)

If the inplace argument is set to True, then the DataFrame is filled in-place.

When the argument is set to True, the original DataFrame object is mutated and None is returned.

# Incorrectly creating a DataFrame or a Series

The error also occurs when you incorrectly try to create a DataFrame or a Series.

Here is an example.

main.py
import pandas as pd # ⛔️ ValueError: Length of values (2) does not match length of index (3) df = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4], 'C': [5, 6] }, index=['X', 'Y', 'Z'])

Each column in the DataFrame has 2 rows (values), however, we specified 3 values in the index list.

To solve the error, we have to only specify 2 values in the index list or add a row to each column (A, B and C).

main.py
import pandas as pd df = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4], 'C': [5, 6] }, index=['X', 'Y']) # A B C # X 1 3 5 # Y 2 4 6 print(df)
The code for this article is available on GitHub

Now each column in the DataFrame has 2 rows and the index contains 2 items, so the issue is resolved.

You might also encounter the error when creating a Series.

main.py
import pandas as pd # ⛔️ ValueError: Length of values (3) does not match length of index (2) series = pd.Series( [1, 2, 3], name='ser', index=['A', 'B'] )

The list we passed to the pandas.Series() constructor contains 3 elements, however, the index list only contains 2.

To solve the error, make sure the index list also has 3 elements.

main.py
import pandas as pd series = pd.Series( [1, 2, 3], name='ser', index=['A', 'B', 'C'] ) # A 1 # B 2 # C 3 # Name: ser, dtype: int64 print(series)
The code for this article is available on GitHub

Now the list we passed to the pandas.Series class contains 3 elements and the index list also contains 3 elements, so everything works as expected.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.