Home > Back-end >  check if list in column cell is empty
check if list in column cell is empty

Time:04-17

I'm trying to get the indexes of rows in which cells in specific column contains en empty list. For instance, if the dataframe holds the following table:

|'A'      |  'B'  |
|--------------------
|[1, 2, 3]|   3   |
|    []   |   2   |
|  [2, 5] |   4   |

the result should be the index 1.

Here's a command I tried:

dataframe.loc[len(dataframe['A']) == 0, 'A'].index

It yielded the error message 'KeyError: False'.
I'd love to get ideas about what could be wrong in this command.

Thanks!

CodePudding user response:

You're almost there, just some small tweaks.

dataframe[ dataframe[ 'A' ].apply( len ) == 0 ].index

The problem with your implementation is that len( dataframe[ 'A' ] ) returns the number of rows in column A, instead of applying len to each of the elements in dataframe[ 'A' ].

  • Related