繁体   English   中英

如何使用 Pandas 检查同一表中两个字段之间的最近匹配值并将数据添加到第三个字段?

[英]How to check the nearest matching value between two fields in same table and add data to the third field using Pandas?

我有一张桌子:

指数 月_1 月_2 有薪酬的
01 12 10
02 09 03
03 02 04
04 01 08

output 应该是:

指数 月_1 月_2 有薪酬的
01 12 10 是的
02 09 03
03 02 04 是的
04 01 08

逻辑:在 Month_1 和 Month_2 附近的付费字段中添加“是”

您可以减去列,获取绝对值并比较是否等于或小于阈值,例如2 ,然后在numpy.where中设置值:

df['Paid'] = np.where(df['Month_1'].sub(df['Month_2']).abs().le(2), 'Yes','')
print (df)
  Index  Month_1  Month_2 Paid
0    01       12       10  Yes
1    02        9        3     
2    03        2        4  Yes
3    04        1        8     

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM