Pandas DataFrame row selection where column is not empty list

I have a pandas DataFrame where one column contains lists and would like to select rows in which the lists are not empty.

Sample data:

df = pd.DataFrame({'letter': ["a", "b", "c", "d", "e"], 
               'my_list':[[0,1,2],[1,2],[],[],[0,1]]})

df
    letter   my_list
0   a   [0, 1, 2]
1   b   [1, 2]
2   c   []
3   d   []
4   e   [0, 1]

What I need:

df
    letter  my_list
0   a   [0, 1, 2]
1   b   [1, 2]
4   e   [0, 1]

What am I trying:

df[df.my_list.map(lambda x: if len(x) !=0)]

... which returns an invalid syntax error. Any suggestions?

+4
source share
2 answers

Empty lists are evaluated to Falsein a boolean context

df[df.my_list.astype(bool)]

  letter    my_list
0      a  [0, 1, 2]
1      b     [1, 2]
4      e     [0, 1]
+8
source

Or you can follow your own logic using the length of the list to determine whether or not to save.

df[df.my_list.str.len().gt(0)]
Out[1707]: 
  letter    my_list
0      a  [0, 1, 2]
1      b     [1, 2]
4      e     [0, 1]
+2
source

Source: https://habr.com/ru/post/1695889/


All Articles