【发布时间】:2023-01-09 15:41:54
【问题描述】:
我有一个带有扁平层次结构的熊猫数据框:
| Level 1 ID | Level 2 ID | Level 3 ID | Level 4 ID | Name | Path |
|---|---|---|---|---|---|
| 1 | null | null | null | Finance | Finance |
| 1 | 4 | null | null | Reporting | Finance > Reporting |
| 1 | 4 | 5 | null | Tax Reporting | Finance > Reporting > Tax Reporting |
我想要做的是根据 Level [] ID 列添加或替换为具有 4 个 Level Name 列的 Level ID 列,如下所示:
| Level 1 Name | Level 2 Name | Level 3 Name | Level 4 Name | Name | Path |
|---|---|---|---|---|---|
| Finance | null | null | null | Finance | Finance |
| Finance | Reporting | null | null | Reporting | Finance > Reporting |
| Finance | Reporting | Tax Reporting | null | Tax Reporting | Finance > Reporting > Tax Reporting |
我会在 Path 列上使用分隔符,但在真实数据框中,有 ID 而不是名称。
我应该如何处理这个问题?
【问题讨论】:
-
“我会在 Path 列上使用分隔符,但在真实的数据框中,有 ID 而不是名称。”这是什么意思?您在路径栏中有类似“财务> 4 > 5”的内容吗?