<p>您可以首先将<code>asset_tree</code>转换为嵌套字典,存储级别之间的关系。这样,您就可以使用递归生成器函数,该函数接收级别行并遍历新树,使用行中的名称获取级别中最右侧名称的id:</p>
<pre><code>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}]
a_tree = {i['id']:i for i in asset_tree}
assets = {'name': ['pipe_1', 'pipe_2', 'pipe_3', 'hydrant_1', 'hydrant_2', 'fountain_1', 'fountain_2'], 'level_1': ['Linear Asset', 'Linear Asset', 'Linear Asset', 'Point Asset', 'Point Asset', 'Point Asset', 'Point Asset'], 'level_2': ['Main', 'Lateral', 'Lateral', 'Hydrant', 'Hydrant', 'Fountain', 'Fountain']}
def to_dict(_id):
return {i['id']:to_dict(i['id']) for i in asset_tree if i['parent_id'] == _id}
new_tree = {i['id']:to_dict(i['id']) for i in asset_tree if i['parent_id'] == -1}
def get_id(d, row, c = []):
if not row:
yield c
else:
for a, b in d.items():
if row[0] == a_tree[a]['name']:
yield from get_id(b, row[1:], c+[a])
result = [dict(zip([*assets, 'tree_id'], [a, *b, next(get_id(new_tree, b))[-1]]))
for a, *b in zip(*assets.values())]
df = pd.DataFrame(result)
</code></pre>
<p>输出:</p>
<pre><code> name level_1 level_2 tree_id
0 pipe_1 Linear Asset Main 3
1 pipe_2 Linear Asset Lateral 2
2 pipe_3 Linear Asset Lateral 2
3 hydrant_1 Point Asset Hydrant 6
4 hydrant_2 Point Asset Hydrant 6
5 fountain_1 Point Asset Fountain 5
6 fountain_2 Point Asset Fountain 5
</code></pre>