0

Goal: To update a cell value of a pandas dataframe with a list of lists of lists

Problem: When attempting to do this, I get an error of "Must have equal len keys and value when setting with an iterable"

Overview: I have a pandas dataframe, to which I am wanting to update a cell value. The cell value to input is a list, of lists of lists. The dataframe is already storing a cell value that is such, but this value needs to be updated to a new list of lists of lists. However, when I attempt to make the cell update, I receive the error noted above. I cannot reproduce this issue is some simplified code, so I wonder if it has to do with how long the list is - 40+ lists within lists.

My code to perform this action looks something like this, though as I said, this will not reproduce the issue, as I suspect it has to do with the length of the list:

import pandas

df = pandas.DataFrame(data={'First':['Bob','Bobby'], 'Last': ['Bobberson', 'Bobson']})

for i in df.index:
    df.loc[i, 'First'] = [[[1,3], [4,5]],[[2,3],[4,5]]]
spareTimeCoder
  • 212
  • 2
  • 12

1 Answers1

1

Try replacing .loc with .at:

for i in df.index:
    df.at[i, 'First'] = [[[1,3], [4,5]],[[2,3],[4,5]]]

See more details about why .loc has a problem here

perl
  • 9,826
  • 1
  • 10
  • 22