我有成百上千的行看起来像这样(实际上有更多的数据,但我试图简化我一直在尝试的想法)
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']='已感染'
可以将^{} 与^{} 结合使用:
只是用
.loc
相关问题 更多 >
编程相关推荐