0

I want to drop rows from a dataframe, based on the condition that the value of a specific column is in a list. If this is not the case I want the row to be dropped.

Do you have any suggestions? Thanks in advance

As an example, if the value in column 'C' is not inside the list l, I want to drop the entire row

import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0,100,size=(20, 4)), columns=list('ABCD')) l = [4, 6, 23, 45, 79] 
4
  • 2
    Can you please provide some text data, what you have tried, and what results you are expecting? Commented May 10, 2022 at 12:43
  • 1
    Hi and welcome on SO. It will be great if you can have a look at How to Ask and then try to produce a minimal reproducible example. Commented May 10, 2022 at 12:45
  • Or, what is the condition? Which value? Edit the question. Commented May 10, 2022 at 12:45
  • oh sorry, edited now Commented May 10, 2022 at 12:50

2 Answers 2

1
df = df[df.apply(lambda x: any(x.isin(l)), axis=1)] 

if only one column must be

and if, only C:

df[df.apply(lambda x: x["C"] in l, axis=1)] 

or, if all columns:

df = df[df.apply(lambda x: all(x.isin(l)), axis=1)] 
Sign up to request clarification or add additional context in comments.

Comments

0

You can try like this:

df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], columns=["a", "b"])

df[(df.a.isin([3]) == False)]

This will only leave those lines in the data frame, whose a-value is not in the list given to isin, i.e. that where it is not 3.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.