代码之家  ›  专栏  ›  技术社区  ›  DeeeeRoy

如何在另一个数据帧上用vlookup程序创建新的pandas列

  •  0
  • DeeeeRoy  · 技术社区  · 6 年前

    我有一个像这样的数据帧。它将用于使用两个分类变量映射值。也许把它改成字典会更好。 Index, and Column names represent possible values taken by two categorical variables in another data-frame

    第二个数据帧非常大,屏幕截图如下所示。我想从分类变量中获取值,基于第一个数据帧创建一个新属性(列)。

    例如。。。

    FICO\ U cat为(700720)且OrigLTV\ U cat为(75,80)的行将接收值5。

    有没有有效的方法?

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   Vishnu Kunchur    6 年前

    如果列标签是 FICO_cat 价值观和你的 Index OrigLTV_cat ,这应该起作用:

    给定一个数据帧 df

             780+  (740,780)  (720,740)
    (60,70)     3          3          3
    (70,75)     4          5          4
    (75,80)     3          1          2
    

    执行:

    df = df.unstack().reset_index()
    df.rename(columns = {'level_0' : 'FICOCat', 'level_1' : 'OrigLTV', 0 : 'value'}, inplace = True)
    

    输出:

         FICOCat  OrigLTV  value
    0       780+  (60,70)      3
    1       780+  (70,75)      4
    2       780+  (75,80)      3
    3  (740,780)  (60,70)      3
    4  (740,780)  (70,75)      5
    5  (740,780)  (75,80)      1
    6  (720,740)  (60,70)      3
    7  (720,740)  (70,75)      4
    8  (720,740)  (75,80)      2