简体   繁体   English

Python Pandas:如何更改值包含特定单词的列中的所有值

[英]Python Pandas: How to change all the values in the column where the value contains a specific word

New to python - I am trying to change all the values in one of the columns in my data frame where the text contains "employed or Employed" word. python 的新手 - 我正在尝试更改我的数据框中其中一个列中的所有值,其中文本包含“已雇用或已雇用”字样。 Should i use the lambda function to loop through the column?我应该使用 lambda function 循环遍历列吗? If no, then what's the most optimal way to do this?如果不是,那么执行此操作的最佳方法是什么?

df = pd.DataFrame([
    ['Self-employed',1,1],
    ['Self employed contract labour',1,1],
    ['Self Employed',1,0],
    ['N/A(Self employed)',1,0],
    ['SELF EMPLOYED',1,0]
], columns=['A', 'B', 'C'])
df

Expected Output:预期 Output:

    ['Self Employed',1,1],
    ['Self Employed',1,1],
    ['Self Employed',1,0],
    ['Self Employed',1,0],
    ['Self Employed',1,0]

Looks like str.contains and boolean indexing should do the trick:看起来str.contains和 boolean 索引应该可以解决问题:

df.loc[df['A'].str.contains('employed', case=False), 'A'] = 'Self Employed'

output: output:

               A  B  C
0  Self Employed  1  1
1  Self Employed  1  1
2  Self Employed  1  0
3  Self Employed  1  0
4  Self Employed  1  0

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

相关问题 熊猫:如何获取一列的所有值,其中另一列的值是特定值 - Pandas: How to get all values for a column, where another column's value is a specific value Pandas:如何删除列值与特定值匹配的行(所有值都是值列表) - Pandas : How to drop a row where column values match with a specific value (all value are list of value) pandas:在列包含特定值的行之后插入一行 - pandas: insert a row after a row where the column contains a specific value Python Pandas选择组,其中特定列包含零 - Python Pandas select group where a specific column contains zeroes Python - 需要删除数据框中的所有数据,其中特定列的值至少包含 1 个字母 - Python - Need to drop all data in the dataframe, where the value of one specific column contains at least 1 letter 使用pandas“where”更改条目的值,该值以列中的所有值为条件 - Using pandas "where" change values of an entry conditioning on all values in the column 如何总结列中包含特定字符串的行中的所有值? - How to sum up all values in a row where the column contains a specific string? Python-合并其中列包含特定值的数据框 - Python - Merge dataframes where column contains a specific value python pandas如果列字符串包含单词标志 - python pandas if column string contains word flag Pandas:如何更改列的所有值? - Pandas: how to change all the values of a column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM