Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

readme.md

ValueError: Length of values does not match length of index

Occurs when assigning a list or array whose length does not match the DataFrame index.

Reproduce

import pandas as pd

data = {
    "name": ["john", "peter"],
    "age": [30, 25]
}

df = pd.DataFrame(data)

# Trying to assign 3 values to a column with 2 rows
df["salary"] = [1000, 2000, 3000]

print(df)

Error Message

ValueError: Length of values does not match length of index

Fix

import pandas as pd

data = {
    "name": ["john", "peter"],
    "age": [30, 25]
}

df = pd.DataFrame(data)

# Match the number of rows
df["salary"] = [1000, 2000]

print(df)

Reflection

Tried to assign more values than rows in the DataFrame. Spent time checking the data before realizing the length mismatch.