Home > Net >  Remove values from columns if a column contains specific string in Python
Remove values from columns if a column contains specific string in Python

Time:07-28

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

CodePudding user response:

Try with :

df.loc[(df.status == 'new'), ['value1', 'value2']] = ''
  • Related