我有一个看起来像这样的df
ID num1 children num2
34 self 1 1
23 none 2 0
85 dependents 3 2
我只想在='depends'num2
中将值替换为中的值。我已经尝试了一些方法,包括但还没有使其正常工作children
num1
mask
mask = (df['num1'] == 2)
df['num1'][mask] = df['children']
理想情况下,输出看起来像这样
ID num1 children num2
34 self 1 1
23 none 2 0
85 dependents 3 3
cond = df['num1'].eq('dependents')
df['num2'] = df['num2'].mask(cond,df['children'])
#df['num2'] = df['children'].where(cond,df['num2'])
df.loc[cond,'num2'] = df['children']
输出量
ID num1 children num2
0 34 self 1 1
1 23 none 2 0
2 85 dependents 3 3
详情
print(cond)
0 False
1 False
2 True
Name: num1, dtype: bool
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句