Python Pandas:Hlookup函数? [英] Python Pandas: Hlookup Function?

查看:152
本文介绍了Python Pandas:Hlookup函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能对此想法有所思考。但是,我试图从另一个df中查找符合特定条件的值。这是第一个DF的示例:

I may be overthinking this. But I am trying to lookup a value from another df when it matches certain criteria. This is an example of the first DF:

City  A  B  C  D
BKN   5  2  3  5
DET   4  1  6  4
WAS   3  2  3  7

这是第二个:

Name  Cri  City  Wanted Val  
P1    B    BKN       ?
P2    D    DET       ?

希望这对我有帮助,但本质上我希望我的 Wanted Val列充满了Cri和City的坐标。因此,对于P1,最终结果最终将是所需值中的2,对于P2,最终结果将是4,依此类推。

Hopefully this makes sense how I have it, but essentially I want my 'Wanted Val' column to be filled with whats at the coordinates for Cri and City. So the final result would end up being, for P1, a 2 in the wanted val, and for P2 a 4, and so on. Any help on this would be appreciated.

推荐答案

您可以使用 查找

You can use lookup

In [3010]: df1
Out[3010]:
  City  A  B  C  D
0  BKN  5  2  3  5
1  DET  4  1  6  4
2  WAS  3  2  3  7

In [3011]: df2
Out[3011]:
  Name Cri City
0   P1   B  BKN
1   P2   D  DET

In [3012]: df2['Wanted-Val'] = df1.set_index('City').lookup(df2.City, df2.Cri)

In [3013]: df2
Out[3013]:
  Name Cri City  Wanted-Val
0   P1   B  BKN           2
1   P2   D  DET           4

这篇关于Python Pandas:Hlookup函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆