Fix error when assigning a list of values to dataframe row

26 Views Asked by At

When I execute this code:

import pandas as pd

# Example DataFrame creation
df = pd.DataFrame({'A': [1, 2, 3]}, index=[0, 1, 2])

# Assuming idx is a valid index in df, for example:
idx = 1

# Store the list as a single value in the DataFrame
df.loc[idx, 'SHAP'] = [1, 2]

print(df)

I get the error: ValueError: Must have equal len keys and value when setting with an iterable how do I fix this? I am using pandas version 2.1.4

0

There are 0 best solutions below