基于多个条件替换多个值

2024-04-26 13:42:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用两个不同列中的另外两个值替换两个不同列中的两个值。例如:

我想替换列valuenull的所有值,其中列variable中的值为name,列source中的值为a。另外,我想保留replace值的来源

variable | value    | source
name       np.NaN     a
name       John       b
email      qq@g.com   a
....       ....      ....

预期产出:

variable | value    | source
name       John       b
name       John       b
email      qq@g.com   a
....       ....      ....

然后我就把复制品扔了。我正试图用np.where做这件事,但没有成功。 我不知道如何进行多重条件和多重替换

df['name'] = np.where(df['variable'] == 'name' and df['source'] == 'a' and df['value'].isnull(), ...) 

也许有更优雅的方式来做到这一点。而且,性能越快越好


Tags: andnamecomsourcedfvalueemailnp