user12907213
user12907213

Reputation:

Select rows having not empty list in pandas

I have the following dataset (name of columns are just an example):

Col1 Col2 Col3 Col4
12   321    42  []
31   42     542 [stop]
65   64     41  []
754  76     431 [python]

How can I select rows having not empty list in Col4 (i.e. second and fourth rows in the above sample)?

Upvotes: 1

Views: 1302

Answers (3)

Derek Eden
Derek Eden

Reputation: 4618

could do something like this:

df[df['Col4'].astype(str) != '[]']

this just converts the column to strings to make it easier to compare an empty list

or:

df[df['Col4'].str.len() != 0]

Upvotes: 1

NYC Coder
NYC Coder

Reputation: 7594

You can check the length of the list:

df = df[df['Col4'].apply(lambda x: len(x)) > 0]

Upvotes: 0

BENY
BENY

Reputation: 323226

Check with

subdf=df[df.Col4.astype(bool)].copy()

Upvotes: 1

Related Questions