简体   繁体   中英

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

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

Try with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM