It seems to me that this question has probably been asked before, if not on SO then elsewhere. I have not been able to find it. Apologies if this is a duplicate...
It turns out that there is indeed a very related question: Python pandas insert list into a cell. It is not a duplicate, though: indeed, the error there is different from the error here. However, the solution there can be applied here.
It has also helpfully been given below: https://stackoverflow.com/a/66852480/5065462.
I am trying to store a list inside a pandas
DataFrame. In short, each row corresponds to some information on a group of people and I have a people_ids
columns which is a list of their ids.
However, I am running into the following error.
ValueError: cannot set using a multi-index selection indexer with a different length than the value
Here is a minimal non-working example.
df = pd.DataFrame({"a": [[1], [1,2]], "b": range(1,3)})
print(df)
df.loc[0, "a"] = [10,2]
print(df)
Running the df.loc
command gives the above error. I get the same error if I replace the lists by tuples and/or use lists/tuples each of the same length in the different rows.
If I remove column b
, then it works fine with both lists and tuples.
Also, if I replace [10,2]
with [10]
, then it runs, but the first entry in column a
is not a list with the single entry 10
, but is a single integer. Interestingly, this happens even with (10,)
in place of [10]
.
My guess is that it thinks the right-hand side is some kind of index, hence the "multi-index error".