Vlookup在Pandas具有近似匹配 [英] Vlookup in Pandas with approximate match
本文介绍了Vlookup在Pandas具有近似匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要对两个熊猫数据框执行vlookup样式操作
I need to do a vlookup style operation on two pandas dataframes
Excel中的Vlookup函数具有一个额外的参数,无论它应该找到近似匹配还是精确匹配.对于完全匹配,我知道我可以使用join函数.但是,如果找到下一个较大的值,我该如何进行近似匹配?
The Vlookup function in Excel has an extra parameter whether it should find an approximate or exact match. For exact match I know I can using the join function. But how would I do the approximate match where I find the next larger value?
例如,如果我有一个标记和等级定义数据框,如下所示:
For instance, if I have a marks and grades definition dataframe, like this:
Student Mark
John 65
Peter 75
Jason 79
还有
Mark Symbol
50 D
60 C # table indicates a mark between 60 and 69 is a C symbol
70 B
80 A
我如何获得这样的表:
Student Mark Symbol
John 65 C
Peter 75 B
Jason 79 B
推荐答案
Use merge_asof
for merge on nearest key
In [2484]: pd.merge_asof(df1, df2, on='Mark')
Out[2484]:
Student Mark Symbol
0 John 65 C
1 Peter 75 B
2 Jason 79 B
详细信息
Details
In [2485]: df1
Out[2485]:
Student Mark
0 John 65
1 Peter 75
2 Jason 79
In [2486]: df2
Out[2486]:
Mark Symbol
0 50 D
1 60 C
2 70 B
3 80 A
这篇关于Vlookup在Pandas具有近似匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文