2

I have a DataFrame with 200 columns.
Some of the rows in columns between 10 and 180 have values between -1 and 0.

I need to remove all rows with these values, but only if they occur in columns between 100 and 180. If these values occur in columns 10 to 99 it is fine and I keep them.

I was thinking to use something like:

df[~df[['col100', 'col101',..., 'col180']].isin([-1, 0]).any(1)]

However, I cannot specify all the column names by hand. What is the right way to do this operation?

2 Answers 2

2

Use loc accessor

If you need between -1 and 0, use;

df[~df.loc[:,'col100':'col180'].apply(lambda x: x.between(-1,0)).any(1)]

If you need just to check if they have -1 and 0 use

df[~df.loc[:,'col100':'col180'].isin([0,-1]).any(1)]

How it works

df.loc[:,'col100':'col180']- Selects the columns between 100 and 180

df.loc[:,'col100':'col180'].isin([0,-1]).any(1): Tests if there is 0 or -1 in the columns
Sign up to request clarification or add additional context in comments.

Comments

1

How about

cols_to_remove = [f'col{n}' for n in range(100,181)]
df[~df[cols_to_remove].isin([-1, 0]).any(1)]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.