简体   繁体   English

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

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

I have one table:我有一张桌子:

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

The output should be: output 应该是:

Index指数 Month_1月_1 Month_2月_2 Paid有薪酬的
01 01 12 12 10 10 Yes是的
02 02 09 09 03 03
03 03 02 02 04 04 Yes是的
04 04 01 01 08 08

Logic: Add 'Yes' to the Paid field whose Month_1 and Month_2 are nearby逻辑:在 Month_1 和 Month_2 附近的付费字段中添加“是”

You can subtract columns, get absolute values and compare if equal or less like threshold, eg 2 and then set values in numpy.where :您可以减去列,获取绝对值并比较是否等于或小于阈值,例如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.

相关问题 Pandas 检查两个数据框列并访问第三个值 - Pandas Check two data frame columns and access third value 如何在 Pandas 中获得最接近的匹配值? - How to get the nearest matching value in Pandas? 如何使用 Python 中的两个字段过滤 Pandas 数据表? - How to filter Pandas data table using two fields in Python? python中使用pandas的两个DataFrame之间的值匹配 - value matching between two DataFrames using pandas in python 熊猫如何添加计数器以匹配两个数据框列之间的行 - Pandas how to add the counters for matching rows between two dataframe columns 使用 Python(最好是 Pandas?)在两个 csv 文件之间匹配数据 - Matching data between two csv files using Python (preferably Pandas?) 加入两个数据框,然后使用 Pandas 组合具有相同名称的字段中的数据 - Joining two dataframes then combining data in fields with same name using Pandas 如何检查两个数据集的匹配列之间的相关性? - How to check correlation between matching columns of two data sets? 在Django中,如何添加两个字段的内容并在第三个字段中显示结果? - In Django, how do I add the contents of two fields and display the results in a third field? Python / Pandas - 如何按两列分组,并计算两行之间第三列的值 - Python/Pandas - How to group by two columns and count rows with value from third column between two numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM