Reason for the error

The length of the index of the pandas DataFrame(i.e length of the column of present DataFrame) which is 10 in this case is not equal to the length of the new list or NumPy array which is 7 in this case.

pd.Index.size!=len(petal_width)

How to Fix: Length of values does not match length of index

In this article we will fix the error: The length of values does not match the length of the index in Python.

Cases of this error occurrence:

Python3




# importing pandas
import pandas as pd
  
sepal_length = [5.1, 4.9, 4.7, 4.6, 5.0, 5.4
                4.6, 5.0, 4.4, 4.9]
sepal_width = [4.6, 5.0, 5.4, 4.6, 5.0, 4.4
               4.9, 5.1, 5.2, 5.3]
petal_length = [3.3, 4.6, 4.7, 5.6, 6.7, 5.0, 4.8]
petal_width = [3.6, 5.6, 5.4, 4.6, 4.4, 5.0, 4.9]
  
# DataFrame with 2 columns
df = pd.DataFrame({'sepal_length(cm)': sepal_length,
                   'sepal_width(cm)': sepal_width})
  
df['petal_length(cm)'] = petal_length
df['petal_width(cm)'] = petal_width
  
print(df)


Output:

ValueError: Length of values (7) does not match length of index (10)

Similar Reads

Reason for the error :

...

Fixing the error:

The length of the index of the pandas DataFrame(i.e length of the column of present DataFrame) which is 10 in this case is not equal to the length of the new list or NumPy array which is 7 in this case....