1

I am trying to remove all the rows from a data frame where a certain condition is met in one column.

Lets say my data frame is this:

index  'Value 1'   'Grade'
1         10         2170A
2         15         2170B
3         10         NCR2170B
4         20         NCR2170A
5         30         NCR2170B

I want the output after filtering to be this

    index  'Value 1'   'Grade'
2         15         2170B
3         10         NCR2170B
5         30         NCR2170B

I have tried many different variations of using list comprehension, looping through each row and evaluating. I can make it work when I use something along the lines of:

data_filtered = data[data['Grade'] == '2170B']

But this obviously misses entries like NCR2170B.

Whenever I try something like:

data_filtered = data['2170B' in data['Grade']]

I get Key Error: 'True'.

I feel like I am missing something very obvious here.

I have also tried using np.where but it just outputs an empty array.

0

1 Answer 1

1

You can use str.contains to check if a string column contains a sub string:

df[df.Grade.str.contains('2170B')]

#index  Value 1    Grade
#1  2        15    2170B
#2  3        10 NCR2170B
#4  5        30 NCR2170B
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much I knew it was something simple that I wasn't aware of. Sorry I am new to python.

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.