【问题标题】:Map 2 df but column to value instead of value to value for each ID将 2 df 但列映射到值而不是每个 ID 的值到值
【发布时间】:2021-02-10 03:32:36
【问题描述】:

我有一个表,其中包含前 3 个原因(表 1)和另一个表,其中包含每个变量所属的类别(表 2)。 我正在尝试将类别箱匹配到表 3 中的原因表中。

Table 1: top 3 reasons, basically which 3 variable was the most important for that Register number

      RegNo    Reason1  Reason2 Reason3
        1111         v3       v2     v6
        2222         v2       v3     v5
        3333         v3       v2     v6
        4444         v3       v6     v2
        5555         v3       v2     v5


Table 2: The category bin for each variable 

    RegNo           v2              v3        v4          v5                 v6
    1111    (0.44, 0.64]    (0.0, 60.0]     missing    (-inf, 3.0]   (102.0, 175.0]
    2222    (-inf, 0.33]    (0.0, 60.0]     welldone   missing       missing
    3333    (0.44, 0.64]    (0.0, 60.0]     rare       missing       missing
    4444    (0.44, 0.64]    (0.0, 60.0]     missing    missing       (20.0, 102.0]
    5555     (0.64, inf]    (0.0, 60.0]     missing    missing       (-inf, 20.0]


Table 3: For each ID, find category bin label and replace reason1,2 & 3 with the labels

       RegNo            Reason1         Reason2         Reason3
        1111         (0.0, 60.0]    (0.44, 0.64]     (102.0, 175.0]
        2222         (-inf, 0.33]    (0.0, 60.0]       missing
        3333         (0.0, 60.0]    (0.44, 0.64]       missing
        4444         (0.0, 60.0]    (20.0, 102.0]    (0.44, 0.64]
        5555         (0.0, 60.0]     (0.64, inf]        missing

【问题讨论】:

    标签: python pandas dataframe dictionary


    【解决方案1】:

    方法

    • 以与join() 一起使用的方式索引两个数据帧
    • 那么它是连接输出中每个原因的pd.concat()
    df1 = pd.read_csv(io.StringIO("""      RegNo    Reason1  Reason2 Reason3
            1111         v3       v2     v6
            2222         v2       v3     v5
            3333         v3       v2     v6
            4444         v3       v6     v2
            5555         v3       v2     v5
    """), sep="\s+")
    
    df2 = pd.read_csv(io.StringIO("""    RegNo           v2              v3        v4          v5                 v6
        1111    (0.44, 0.64]    (0.0, 60.0]     missing    (-inf, 3.0]   (102.0, 175.0]
        2222    (-inf, 0.33]    (0.0, 60.0]     welldone   missing       missing
        3333    (0.44, 0.64]    (0.0, 60.0]     rare       missing       missing
        4444    (0.44, 0.64]    (0.0, 60.0]     missing    missing       (20.0, 102.0]
        5555     (0.64, inf]    (0.0, 60.0]     missing    missing       (-inf, 20.0]
    """),sep="\s\s+", engine="python")
    
    # index main df
    df1 = df1.set_index("RegNo")
    # reshape and index reasons
    dfm = df2.set_index("RegNo").stack()
    df3 = pd.concat([
        # add apprpriate column into index,  don't want othert columns
        (df1.set_index(c, append="True").loc[:,[]]
         # now it's a straight forward join
         .join(dfm.rename_axis(["RegNo",c]).to_frame())
         # cleanup index and rename columns of joined DF
         .droplevel(1).rename(columns={0:c}))
        for c in ["Reason1","Reason2","Reason3"]], axis=1)
    
    RegNo Reason1 Reason2 Reason3
    1111 (0.0, 60.0] (0.44, 0.64] (102.0, 175.0]
    2222 (-inf, 0.33] (0.0, 60.0] missing
    3333 (0.0, 60.0] (0.44, 0.64] missing
    4444 (0.0, 60.0] (20.0, 102.0] (0.44, 0.64]
    5555 (0.0, 60.0] (0.64, inf] missing

    【讨论】:

    • 哇!非常感谢!我试图理解代码的最后 3 行!我如何分解它以查看正在发生的事情的步骤?
    • 代码中的换行符确实构建了方法... step1:df1.set_index(c, append="True") step2:dfm.rename_axis(["RegNo",c]).to_frame() step3:将其组合在一起 step4:cleanup .droplevel(1).rename(columns={0:c})。在所有步骤中,您大多可以单独运行,替换其中一列
    猜你喜欢
    • 2015-08-12
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    相关资源
    最近更新 更多