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

关于Python - 如果另一列具有列表中的值,则 Pandas 会替换列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50309816/

相关文章:

python - Python 子进程中的 ffmpeg - 无法为 'pipe:' 找到合适的输出格式

python - ctypes 中的复数

python - Pandas 合并索引不起作用

pandas - 使用 for 循环迭代 pandas 数据框中的日期范围

python - pytorch model.parameter的形状与模型中的定义不一致

python - pyqtgraph ImageView 在多线程时卡住

python - 与 Python 2.7 中的 ModuleNotFoundError 类似的异常?

python - 统计列中连续True的个数,False时重新开始

python - 在 MultiIndex 中为缺失日期插入 0 值

python-3.x - Pandas 按功能过滤数据帧行