9.1 C
London
Friday, December 20, 2024
HomePythonFix Common Errors in PythonHow to Fix: Length of values does not match length of index

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories