Must have equal len keys and value when setting with iterable

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Must have equal len keys and value when setting with iterable

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.

main.py
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']

value error must have equal len keys and value when setting with iterable

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.

# Use the DataFrame.apply() method to solve the error

You can solve the error by using the DataFrame.apply() method.

main.py
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)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
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]

using dataframe apply method to solve the error

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.

main.py
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)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 11 175.1 1 Bobby 14 180.2 2 Carl 16 190.3 3 Dan 18 1000000.0

apply the function to each row with axis set to 1

# Using list multiplication to solve the error

You can also use the multiplication operator with lists to concatenate the lists and solve the error.

main.py
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)

using multiplication operator to solve the error

The code for this article is available on GitHub

When the multiplication operator is used with a list, the values of the list get repeated N times.

main.py
# [['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y']] print([['X', 'Y'] * 4])

# Using the DataFrame.at() method to solve the error

You can also use the DataFrame.at() method to solve the error.

main.py
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)

using dataframe at method to solve the error

The code for this article is available on GitHub

The DataFrame.at() method enables you to access a single value for a row/column label pair.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.