为什么熊猫替换不会在df切片中起作用? [重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我有这样的数据帧:

| action   | spans | product_id     | created_at      |
|----------|-------|----------------|-----------------|
| sign_in  | 0     | 2201           | 2019/3/10 12:11 |
| order    | 0     | 449            | 2019/4/13 14:58 |

product = {'2201': 'project a'}的dict

我想用qict产品的值替换product_id,其中action为sign_in。

但是代码

df[df['action']=='sign_in']]['product_id'].replace(product, inplace=True)

不行。

谁能帮忙解释一下?谢谢。

python pandas
1个回答
0
投票

我不知道为什么inplace=True不适用于蒙版DF,但你可以通过以下方式实现相同的目标(甚至更好):

df.update(df.loc[df['action']=='sign_in','product_id'].replace(product))
© www.soinside.com 2019 - 2024. All rights reserved.