Last updated: Apr 12, 2024
Reading time·3 min

The Pandas "ValueError: Must have equal len keys and value when setting with
an iterable" occurs when you incorrectly iterate over a DataFrame to try to
set column values.
To solve the error, use the DataFrame.apply() method.
Here is an example of how the error occurs.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [11, 14, 16, 18], 'salary': [175.1, 180.2, 190.3, 210.4], }) df['coords'] = '' print(df) for i in range(len(df)): # ⛔️ ValueError: Must have equal len keys and value when setting with an iterable df.loc[i, 'coords'] = ['X', 'Y']

We tried to use a for loop to iterate over the DataFrame and set the values
of the coords column to a list which caused the error.
DataFrame.apply() method to solve the errorYou can solve the error by using the DataFrame.apply() method.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [11, 14, 16, 18], 'salary': [175.1, 180.2, 190.3, 210.4], }) df['coords'] = '' df['coords'] = df['coords'].apply(lambda x: ['X', 'Y']) print(df)
Running the code sample produces the following output.
name experience salary coords 0 Alice 11 175.1 [X, Y] 1 Bobby 14 180.2 [X, Y] 2 Carl 16 190.3 [X, Y] 3 Dan 18 210.4 [X, Y]

The DataFrame.apply() method applies a function along an axis of the
DataFrame.
By default, the axis argument is set to 0 which means that the function is
applied to each column.
If you need to apply the function to each row of the DataFrame, make sure to
set the axis argument to 1.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [11, 14, 16, 18], 'salary': [175.1, 180.2, 190.3, 210.4], }) df['salary'] = df.apply( lambda x: 1000000 if x['experience'] > 16 else x['salary'], axis=1 ) print(df)
Running the code sample produces the following output.
name experience salary 0 Alice 11 175.1 1 Bobby 14 180.2 2 Carl 16 190.3 3 Dan 18 1000000.0

You can also use the multiplication operator with lists to concatenate the lists and solve the error.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [11, 14, 16, 18], 'salary': [175.1, 180.2, 190.3, 210.4], }) df['coords'] = [['X', 'Y']] * len(df) # name experience salary coords # 0 Alice 11 175.1 [X, Y] # 1 Bobby 14 180.2 [X, Y] # 2 Carl 16 190.3 [X, Y] # 3 Dan 18 210.4 [X, Y] print(df)

When the multiplication operator is used with a list, the values of the list get repeated N times.
# [['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y']] print([['X', 'Y'] * 4])
DataFrame.at() method to solve the errorYou can also use the DataFrame.at() method to solve the error.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [11, 14, 16, 18], 'salary': [175.1, 180.2, 190.3, 210.4], }) df['coords'] = '' for i in range(len(df)): df.at[i, 'coords'] = ['X', 'Y'] # name experience salary coords # 0 Alice 11 175.1 [X, Y] # 1 Bobby 14 180.2 [X, Y] # 2 Carl 16 190.3 [X, Y] # 3 Dan 18 210.4 [X, Y] print(df)

The DataFrame.at() method enables you to access a single value for a
row/column label pair.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [11, 14, 16, 18], 'salary': [175.1, 180.2, 190.3, 210.4], }) print(df.at[0, 'name']) # 👉️ Alice
The method uses label-based lookups, like the .loc indexer.
You can learn more about the related topics by checking out the following tutorials: