我有一个类别树,由以下内容表示
import pandas as pd
asset_tree = [
{'id': 1, 'name': 'Linear Asset', 'parent_id': -1},
{'id': 2, 'name': 'Lateral', 'parent_id': 1},
{'id': 3, 'name': 'Main', 'parent_id': 1},
{'id': 4, 'name': 'Point Asset', 'parent_id': -1},
{'id': 5, 'name': 'Fountain', 'parent_id': 4},
{'id': 6, 'name': 'Hydrant', 'parent_id': 4}
]
tree = pd.DataFrame(asset_tree)
print(tree)
这给了我一个数据帧,如下所示:
id name parent_id
0 1 Linear Asset -1
1 2 Lateral 1
2 3 Main 1
3 4 Point Asset -1
4 5 Fountain 4
5 6 Hydrant 4
树中最高的节点的父节点id等于-1,因此该树可以用图形表示如下:
Linear Asset
| - Lateral
| - Main
Point Asset
| - Fountain
| - Hydrant
我需要生成以下数据帧
id name parent_id flat_name
0 1 Linear Asset -1 Linear Asset
1 2 Lateral 1 Linear Asset : Lateral
2 3 Main 1 Linear Asset : Main
3 4 Point Asset -1 Point Asset
4 5 Fountain 4 Point Asset : Fountain
5 6 Hydrant 4 Point Asset : Hydrant
树是动态生成的,可以有任意数量的级别,因此下面的树
asset_tree = [
{'id': 1, 'name': 'Linear Asset', 'parent_id': -1},
{'id': 2, 'name': 'Lateral', 'parent_id': 1},
{'id': 3, 'name': 'Main', 'parent_id': 1},
{'id': 4, 'name': 'Point Asset', 'parent_id': -1},
{'id': 5, 'name': 'Fountain', 'parent_id': 4},
{'id': 6, 'name': 'Hydrant', 'parent_id': 4},
{'id': 7, 'name': 'Steel', 'parent_id': 2},
{'id': 8, 'name': 'Plastic', 'parent_id': 2},
{'id': 9, 'name': 'Steel', 'parent_id': 3},
{'id': 10, 'name': 'Plastic', 'parent_id': 3}
]
应产生以下结果:
id name parent_id flat_name
0 1 Linear Asset -1 Linear Asset
1 2 Lateral 1 Linear Asset : Lateral
2 3 Main 1 Linear Asset : Main
3 4 Point Asset -1 Point Asset
4 5 Fountain 4 Point Asset : Fountain
5 6 Hydrant 4 Point Asset : Hydrant
6 7 Steel 2 Linear Asset : Lateral : Steel
7 8 Plastic 2 Linear Asset : Lateral : Plastic
8 9 Steel 3 Linear Asset : Main : Steel
9 10 Plastic 3 Linear Asset : Main : Plastic
这是一个网络问题,请尝试
networkx
:输出:
可以使用递归查找父id的路径:
输出:
在较大的
asset_tree
上:输出:
这里有一个递归的
apply
函数来实现这一点。函数接受一个id
并通过树返回其“路径”:要使用,请致电:
在第二个示例中使用后的
df
:OP注意到上面的函数显式地引用了在函数范围之外定义的
df
变量。因此,如果您将数据帧称为不同的名称,或者您希望在许多数据帧上调用此名称,这可能会导致问题。一种修复方法是将apply
函数转变为更像私人助手的函数,并创建一个调用它的外部(更用户友好)函数:然后致电:
另外,请注意,我曾经使用
row = df.iloc[ID - 1, :]
来标识行,这在本例中有效,但取决于id
比索引大一This approach更一般相关问题 更多 >
编程相关推荐