【问题标题】:How to perform multiple lookups based on column values and populate empty column?如何根据列值执行多次查找并填充空列?
【发布时间】:2023-01-11 15:39:29
【问题描述】:

我有一个包含 4 列的数据框。我必须执行一些查找,然后在其中一列中分配值。这是数据示例:

CategoryId  ParentCategoryId  SourceCategoryId  SourceParentCategoryId
         1                                 100                       0
         2                                 101                       0
         3               9.0               102                     108
         4              20.0               103                     100
         5               4.0               104                     103
         6                                 105                     103
         7                                 106                     103
         8                                 107                     103
         9                                 108                       0
        10                                 109                     108
        11                                 110                     103
        12                                 111                     103
        13                                 112                     103
        14                                 113                     100
        15                                 114                     113
        16                                 115                     113
        17                                 116                     113
        18                                 117                     113
        19                                 118                     113
        20                                 100                     113

我正在尝试使用下一个逻辑填充 ParentCategoryId 列中的值: 对于每一行,我们将查找 SourceParentCategoryId 中的值,如果它为零,我们将跳过它。如果 SourceParentCategoryId 中的值不同于第 3 行中的零,我们可以看到 SourceParentCategoryId 为 108。然后我需要在 SourceCategoryId 中查找该值并确定它属于哪个 CategoryId。在我提供的示例中,我们可以看到 CategoryId 的 CategoryId 为 9,这是我需要插入 ParentCategoryId 列第 3 行的值。

使用相同的逻辑,第 4 行的 SourceParentCategoryId = 100 基于 SourceCategoryId 的 CategoryId = 20,然后在第 4 行的 ParentCategoryId 列中添加值 20。

我已经使用 Python 好几年了,并且已经对此进行了大量研究,但是,我仍然不明白如何开始解决这个问题。

你能帮我解决这个问题或者至少给我一些提示吗?

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    假设您在 ParentCategoryId 列中还没有数据,您可以使用以下方法:

    • 自我离开加入SourceParentCategoryId == SourceCategoryId 上的数据框。
    • 如果有多个匹配,自连接可能有重复的记录。通过保留第一条记录来限制这一点。
    • SourceParentCategoryId == 0 的重置值。
    df = df.merge(df, left_on="SourceParentCategoryId", right_on="SourceCategoryId", how="left", suffixes=("", "_delete"))
    
    # If there are multiple matches, then the self join may have repeated records. Limit this by retaining first record.
    df = df.groupby(["CategoryId", "SourceCategoryId", "SourceParentCategoryId"])["CategoryId_delete"].first().reset_index()
    
    # Reset values for `SourceParentCategoryId == 0`.
    df = df.rename(columns={"CategoryId_delete": "ParentCategoryId"})
    
    df["ParentCategoryId"] = df.apply(lambda row: None if row["SourceParentCategoryId"] == 0 else row["ParentCategoryId"], axis=1)
    

    输出:

        CategoryId  SourceCategoryId  SourceParentCategoryId  ParentCategoryId
    0            1               100                       0               NaN
    1            2               101                       0               NaN
    2            3               102                     108              9.00
    3            4               103                     100              1.00
    4            5               104                     103              4.00
    5            6               105                     103              4.00
    6            7               106                     103              4.00
    7            8               107                     103              4.00
    8            9               108                       0               NaN
    9           10               109                     108              9.00
    10          11               110                     103              4.00
    11          12               111                     103              4.00
    12          13               112                     103              4.00
    13          14               113                     100              1.00
    14          15               114                     113             14.00
    15          16               115                     113             14.00
    16          17               116                     113             14.00
    17          18               117                     113             14.00
    18          19               118                     113             14.00
    19          20               100                     113             14.00
    

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 2019-10-22
      • 2021-09-18
      • 2023-02-05
      相关资源
      最近更新 更多