[英]Pandas: How to get all values for a column, where another column's value is a specific value
[英]Python Pandas: How to change all the values in the column where the value contains a specific word
python 的新手 - 我正在嘗試更改我的數據框中其中一個列中的所有值,其中文本包含“已雇用或已雇用”字樣。 我應該使用 lambda function 循環遍歷列嗎? 如果不是,那么執行此操作的最佳方法是什么?
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
預期 Output:
['Self Employed',1,1],
['Self Employed',1,1],
['Self Employed',1,0],
['Self Employed',1,0],
['Self Employed',1,0]
看起來str.contains
和 boolean 索引應該可以解決問題:
df.loc[df['A'].str.contains('employed', case=False), 'A'] = 'Self Employed'
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.