【问题标题】:Given 2 data frames search for matching value and return value in second data frame给定 2 个数据帧在第二个数据帧中搜索匹配值并返回值
【发布时间】:2020-10-19 00:33:43
【问题描述】:

给定 2 个像链接示例这样的数据框,我需要将 df2 的“索引收入”添加到 df1。我需要通过 df2 中的 df1 组合键进行搜索,如果有匹配项,则将该值返回到 df1 中的新列中。 df1 和 df2 中的实例数不相等,df1 中大约有 700 行 df2 中有 1000 行。

我可以通过 vlookup 在 excel 中执行此操作,但我现在正尝试将其应用于 python 代码。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这应该可以解决您的问题:

    df1.merge(df2, how='left', on='combind_key')
    

    这(left 加入)将为您提供df1 的所有记录以及来自df2 的匹配记录。

    【讨论】:

      【解决方案2】:

      https://www.geeksforgeeks.org/how-to-do-a-vlookup-in-python-using-pandas/

      这是一个使用连接的答案。我将 df2 修改为仅包含有用的列,然后使用 pandas left join。

      Left_join = pd.merge(df,  
                           zip_df,  
                           on ='State County',  
                           how ='left') 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-09
        • 2012-08-13
        相关资源
        最近更新 更多