[英]Changing some values in df column based on another
我有一个包含多个列和值的 df。 说:
ID | 姓名 | 成本 |
---|---|---|
123 | 乔 | 10 美元 |
345 | 贝拉 | 20 美元 |
567 | 不理我 | 5000 美元 |
我还有一个要忽略的已定义名称列表。 在这个例子中,它包含一个值,但它可以有更多。
names_to_ignore = ['ignoreme']
目标是当Name
在忽略列表中时,用 null 替换所有成本值。
我试过了:
#aligning conventions
df = df.apply(lambda var: var.lower())
ignore_set = [x.lower() for x in ignore_set]
#ignoring
df.loc[df['Name'] in ignore_set, 'Cost'] = ''
但它没有用。 我得到:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
有什么想法吗?
尝试:
names_to_ignore = ['ignoreme','IgnoreMe']
最后:
c=df['Name'].isin(names_to_ignore) #checking if this condition satisfies or not
df.loc[c,'Cost']=float('NaN')
或者
通过np.where()
:
#import numpy as np
df['Cost']=np.where(c,np.nan,c)
或者
通过mask()
:
df['Cost']=df['Cost'].mask(c)
或者
通过where()
:
df['Cost']=df['Cost'].where(~c)
你可以试试np.where()
。
df['cost'] = np.where( (n for n in df['Name'] if n in names_to_ignore), None, df['cost'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.