4

I have a DataFrame that looks like this:

A    B    C    D
foo  foo  foo  ['list_value']
bar  bar  bar  ['list_value', 'another_list_value']
baz  baz  baz  []

How can I filter out the empty list? I'm trying .isin but it throws me an error:

df[df['D'].isin([])]

SystemError: <built-in method view of numpy.ndarray object at 0x0000017ECA74BF30> returned a result with an error set

I also checked this question but couldn't figure out how to implement in a DataFrame context.

Any help would be very appreciated.

1 Answer 1

6

Convert lists to boolean by astype, empty lists return Falses, so filtering working nice:

df1 = df[df['D'].astype(bool)]
print (df1)
     A    B    C                                 D
0  foo  foo  foo                      [list_value]
1  bar  bar  bar  [list_value, another_list_value]

Another solution is filter by length with Series.str.len - it working with all iterables:

df1 = df[df['D'].str.len() != 0]
Sign up to request clarification or add additional context in comments.

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.