如果另一列的值在lis中,则替换列中的值

2024-10-05 10:39:57 发布

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

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

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']='已感染'


Tags: 区域indexstatuslocationhealthyarea4areasarea1
2条回答

可以将^{}^{}结合使用:

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

只是用.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

相关问题 更多 >

    热门问题