[英]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.