Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter rows containing empty lists in a pandas DataFrame?

Tags:

python

pandas

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.

like image 761
Vinícius Alcântara Avatar asked Sep 05 '25 03:09

Vinícius Alcântara


1 Answers

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]
like image 87
jezrael Avatar answered Sep 07 '25 20:09

jezrael