1

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)?

3 Answers 3

1

Check with

subdf=df[df.Col4.astype(bool)].copy()
Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

0

You can check the length of the list:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.