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.
Convert lists to boolean by astype
, empty lists return False
s, 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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With