Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rows containg empty list in a Data Frame Columns in Python

I have a data frame like this:

    test = pd.DataFrame(columns=['A','B'])
    test.loc[0,'A'] = 'a'
    test.loc[0,'B'] = []
    test.loc[1,'A'] = 'b'
    test.loc[1,'B'] = ['a','b']

I want to get another data frame which contains the rows when column 'B' containing an empty list. What's the pythonic way of doing it?

    A    B
0   a   []

Many thanks

like image 578
Sapling Avatar asked Mar 09 '23 22:03

Sapling


2 Answers

As empty lists are going to be interpreted as collections to be processed in a vectorized way, I don't see any way to test it but to drill down to an apply call:

test.B.apply(lambda c: c==[])
Out[71]: 
0     True
1    False
Name: B, dtype: bool

test[test.B.apply(lambda c: c==[])]
Out[72]: 
   A   B
0  a  []
like image 103
Zeugma Avatar answered Apr 30 '23 20:04

Zeugma


Empty lists translate to False as a boolean. Using ~ to convert that to True is a simple trick. It's easy to read and runs pretty fast for me. Definitely faster than using len().

test[~test['B'].astype(bool)]

   A   B
0  a  []
like image 36
grove80904 Avatar answered Apr 30 '23 22:04

grove80904