1

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".


Sam OT
  • 420
  • 1
  • 4
  • 19
  • ya, error is different, because old question. – jezrael Mar 29 '21 at 10:37
  • Yeah, exactly `:-)` -- probably best that this one is reopened, but I guess it doesn't really matter. Experts will surely immediately see the relation between the two questions, but beginners like me may not. Indeed, different commands are being used and different errors are being given `:-)` – Sam OT Mar 29 '21 at 10:40
  • 1
    btw, I think working with `list`s in pandas is not [good idea](https://stackoverflow.com/a/52563718/2901002). – jezrael Mar 29 '21 at 10:41
  • @jezrael Thank you for the suggestion _with justification_. So often high-rep people say "this is a bad idea" but refuse to elaborate. Use newbies are supposed to just take everything on authority. Is using `tuples` instead of `lists` ok? I am using a package which takes some 'ratings' lists and outputs new 'ratings' lists; these 'ratings' are `floats`. I could, of course, _store_ the data as a `numpy` array and then convert to a list/tuple to pass to the package's function. This is a lot of conversion, though. Would you recommend this? – Sam OT Mar 29 '21 at 11:02

1 Answers1

1

Use df.at as it can only access a single value at a time so you can easily assign a list.

df.at[0, "a"] = [10,2]

Hamza usman ghani
  • 2,264
  • 5
  • 19