简体   繁体   English

如果列包含 Python 中的特定字符串,则从列中删除值

[英]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'如果状态列包含字符串“新”,我想删除 value1 和 value2 列中的值

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.我不想要一个 NaN 值,但我希望有一个空白。 Any suggestion is appreciated任何建议表示赞赏

Doing正在做

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

Try with:尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 中的条件从特定列中删除值 - Remove values from specific columns with a condition in Python Python pandas dataframe:在数组列中,如果第一项包含特定字符串,则从数组中删除该项 - Python pandas dataframe : In an array column, if first item contains specific string then remove that item from array 从名称包含特定字符串的列创建新列 - creating new column from columns whose name contains a specific string 移动行值包含特定字符串到 Python 中的新列 - Moving row values contains specific string to new column in Python Python - 如果列名包含特定字符串,则更改该列中的值,否则值保持不变 - Python - If column name contains a specific string then change the values in that column otherwise the values remain 如何选择在python中其列值之一包含特定字符串的行? - how to select rows which one of its columns values contains specific string in python? 如何从还包含文本值的列中的字符串值中删除“.0”或小数点? - How to remove '.0', or the decimal point from string values in a column that also contains values that are text? Python/Pandas 删除包含特定字符串的字符串的开头 - Python/Pandas remove the start of a string which contains a specific string 如何删除 Python 字符串列表的特定列? - How to remove specific column of Python string list? Python 删除 dataframe 列中的特定字符串 - Python Remove specific string in dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM