One error you may encounter when using pandas is:
ValueError: Length of values does not match length of index
This error occurs when you attempt to assign a NumPy array of values to a new column in a pandas DataFrame, yet the length of the array does not match the current length of the index.
The following example shows how to fix this error in practice.
How to Reproduce the Error
Suppose we have the following pandas DataFrame:
import pandas as pd #define DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14], 'assists': [5, 7, 13, 12]}) #view DataFrame print(df) points assists 0 25 5 1 12 7 2 15 13 3 14 12
Now suppose we attempt to add a new column called ‘rebounds’ as a NumPy array:
import numpy as np
#attempt to add 'rebounds' column
df['rebounds'] = np.array([3, 3, 7])
ValueError: Length of values (3) does not match length of index (4)
We receive a ValueError because we attempt to add a NumPy array with a length of 3 to a DataFrame that has an index with a length of 4.
How to Fix the Error
The easiest way to fix this error is to simply create a new column using a pandas Series as opposed to a NumPy array.
By default, if the length of the pandas Series does not match the length of the index of the DataFrame then NaN values will be filled in:
#create 'rebounds' column
df['rebounds'] = pd.Series([3, 3, 7])
#view updated DataFrame
df
points assists rebounds
0 25 5 3.0
1 12 7 3.0
2 15 13 7.0
3 14 12 NaN
Using a pandas Series, we’re able to successfully add the ‘rebounds’ column and the missing values are simply filled in with NaN.
Note that we can quickly convert the NaN values to some other value (such as zero) using the fillna() method as follows:
#fill in NaN values with zero
df = df.fillna(0)
#view updated DataFrame
df
points assists rebounds
0 25 5 3.0
1 12 7 3.0
2 15 13 7.0
3 14 12 0.0
Notice that the NaN value has been converted to a zero.
Additional Resources
The following tutorials explain how to fix other common errors in Python:
How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes