13

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

Example 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'd like:

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

What I'm trying:

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

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

3 Answers 3

24

Empty lists evaluate to False in 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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I just got this to work df[df.my_list.map(lambda x: len(x) !=0)]. But your solution is much better.
5

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

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]

Comments

0

Another option I found helpful is to 'filter' the dataframe like this:

df = df [df ['my_list'] != '']

The != '' is the operation what you want to filter. In this case: no rows where the cell value is None / empty. In other cases it could be simply changed, e.g. list must be longer then 3 df[ len(df ['my_list']) > 3, or first value has to be bigger than 2 df [df ['my_list'][0] > 2], or many other conditions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.