Skip to main content
 首页 » 编程设计

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

2025年05月04日13yxwkf

我有数十万行看起来像这样(实际上有比这更多的数据,但我试图简化我一直在尝试的想法)......

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     ....          .... 

我正在尝试根据某个区域是否被感染来更新 status 列。所以我基本上列出了感染区域:

infected_areas = ['area5', 'area8'] 

然后我要做的是查看所有行(或者实际上只是“健康”行),如果其中任何行与我的 infected_areas 列表中的匹配,则将 status 更改为 infected。

所以对于我上面的例子,输出应该是这样的:

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'].isin(location)]['status'] = 'infected'

请您参考如下方法:

只需使用.loc

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