0

I would like to remove values in the value1 and value2 column if the status column contains the string 'new'

Data

id  date        location    status  value1  value2  
CC  1/1/2022    ny          new     12      1   
CC  4/1/2022    ny          new     1       1   
CC  7/1/2022    ny          new     1       1   
CC  10/1/2022   ny          new     1       2   
CC  1/1/2023    ny          ok      1       2   

Desired

id  date        location    status  value1  value2  
CC  1/1/2022    ny          new         
CC  4/1/2022    ny          new         
CC  7/1/2022    ny          new         
CC  10/1/2022   ny          new         
CC  1/1/2023    ny          ok      1       2   
    

I do not want a NaN value, but I wish to have a blanks. Any suggestion is appreciated

Doing

df.loc[(df.status == 'new')  'value1', 'value2']= np.nan

1 Answer 1

4

Try with :

df.loc[(df.status == 'new'), ['value1', 'value2']] = ''
Sign up to request clarification or add additional context in comments.

4 Comments

"I do not want with a NaN value but wish to have blanks"
Use '' instead of np.nan. Two single quotes no space. to assign a blank string.
Hi @BENY I tried adding more columns besides 2 and this doesn't work. Isn't the logic the same as you add more columns?
@Lynn df.loc[(df.status == 'new'), ['value1', 'value2','value3']] = ''

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.