使用 python pandas 查找另一个数据框并返回相应的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17450857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:58:18  来源:igfitidea点击:

using python pandas lookup another dataframe and return corresponding values

pythonpandas

提问by richie

I have two dataframes;

我有两个数据框;

df1as;

df1作为;

Name      Role
Jim       Accounts
Sam       Purchase
Rhea      Sales

df2as;

df2作为;

Name     Date
Jim      1/1/2000
Jim      2/1/2000
Jim      3/1/2000
Sam      1/1/2000
Sam      2/1/2000
Rhea     1/1/2000
Rhea     2/1/2000

I want to lookup df1and have the output as;

我想查找df1并将输出作为;

    Name     Date          Role
    Jim      1/1/2000      Accounts
    Jim      2/1/2000      Accounts
    Jim      3/1/2000      Accounts
    Sam      1/1/2000      Purchase
    Sam      2/1/2000      Purchase
    Rhea     1/1/2000      Sales
    Rhea     2/1/2000      Sales

I'm unable to figure out Pandas' lookup feature.

我无法弄清楚 Pandas 的查找功能。

回答by Boud

Use mergefunction:

使用merge功能:

df2.merge(df1)