简体   繁体   中英

Dropping pandas rows based on lenght of list in a column

I have a pandas dataframe, one of the columns contains list of values like

df = pd.DataFrame({'A': [11,22,33],
               'B': [[4,5],[10,11,12], []]})

Now I want to drop all the rows that have empty lists in column 'B', Can anyone help please? Thanks

Using str.len

df[df.B.str.len()!=0]
Out[223]: 
    A             B
0  11        [4, 5]
1  22  [10, 11, 12]

Or

df[df.B.astype(bool)]
Out[225]: 
    A             B
0  11        [4, 5]
1  22  [10, 11, 12]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM