Python-如果另一列具有列表中的值,则替换列中的值

Python-如果另一列具有列表中的值,则替换列中的值,python,pandas,Python,Pandas,我有成百上千的行,看起来像这样实际上有更多的数据,但我试图简化我一直在尝试的想法 index status location 0 infected area5 1 healthy area6 2 healthy area3 3 infected area8 4 healthy area1 5 healthy area8 6 healthy area

我有成百上千的行,看起来像这样实际上有更多的数据,但我试图简化我一直在尝试的想法

index  status       location
0      infected     area5
1      healthy      area6
2      healthy      area3
3      infected     area8
4      healthy      area1
5      healthy      area8
6      healthy      area5
7      healthy      area2
8      healthy      area4
9      healthy      area10
10     ....          ....
我正在尝试根据某个区域是否受到感染来更新状态列。所以我基本上列了一张感染地区的清单:

infected_areas = ['area5', 'area8']
然后我要做的是查看所有行,或者仅仅是“健康”行,如果其中任何一行与我的“感染区域”列表中的内容匹配,则将该状态更改为“已感染”

因此,在上面的示例中,输出应该如下所示:

index  status       location
0      infected     area5
1      healthy      area6
2      healthy      area3
3      infected     area8
4      healthy      area1
5      infected     area8
6      infected     area5
7      healthy      area2
8      healthy      area4
9      healthy      area10
10     ....          ....
以下是我一直在做的工作,但没有取得任何进展:

`df[df['location'].isinlocation]['status']='infected'

您可以与以下内容结合使用:

您可以结合使用:

只是在使用.loc

只是在使用.loc


这里没有代码。这是熊猫的问题吗?是的,对不起。它和熊猫在一起。我尝试了.isin,但没有成功。这里没有代码。这是熊猫的问题吗?是的,对不起。它和熊猫在一起。我试了一下,但没有成功。
infected_areas = ['area5', 'area8']

df.status.where(
    ~df.location.str.strip().isin(infected_areas),
    other='infected',
    inplace=True)
>>> df
    index   status  location
0   0   infected    area5
1   1   healthy area6
2   2   healthy area3
3   3   infected    area8
4   4   healthy area1
5   5   infected    area8
6   6   infected    area5
7   7   healthy area2
8   8   healthy area4
9   9   healthy area10
df.loc[df.location.isin(infected_areas),'status']='infected'
df
Out[49]: 
   index    status location
0      0  infected    area5
1      1   healthy    area6
2      2   healthy    area3
3      3  infected    area8
4      4   healthy    area1
5      5  infected    area8
6      6  infected    area5
7      7   healthy    area2
8      8   healthy    area4
9      9   healthy   area10