用pandas查找树中叶节点的所有祖先 [英] Find all the ancestors of leaf nodes in a tree with pandas

查看:70
本文介绍了用pandas查找树中叶节点的所有祖先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列父"和子"的表.这是从 SAP (ERP) 下载的 SETNODE 表.需要在 python 中创建一个数据框,它具有每个级别,因为它是相对于它的父级和之前所有级别的自己的列.

I have a table that has two columns, 'parent' and 'child'. This is a download from SAP (ERP) for SETNODE table. Need to create a dataframe in python that has each level as it's own column in respect to it's parent and all levels before.

在python 3+中.

In python 3+.

完整关系的级别数未知(或总是在变化),因此无法始终定义最大级别.我想创建一个完整的数据框表,显示所有级别的所有父/子关系.目前它大约有 15 个级别,但如果使用我使用的其他数据,它可能会达到 20 个或更多.

There are an unknown (or always changing) number of levels for the full relationship so that max level can't always be defined. I would like to create a full dataframe table that shows ALL parent/child relationships for all levels. Right now it's about 15 levels but it can probably go up to 20 or more with other data I work with.

例如(example_df)的两列:

For example (example_df) of the two columns:

example_df = pd.DataFrame({'parent:['a','a','b','c','c','f'],'child':['b','c','d','f','g','h']})

给出输出数据框(solution_example):

To give output dataframe (solution_example):

solution_example = pd.DataFrame({'child':['h','f','d'],'parent_1':['a','a','a'],'parent_2':['c','c','b'],'parent_3':['f', 'none', 'none']})

推荐答案

这可以使用 networkx 库解决.首先,从DataFrame构建一个有向图,然后找到叶子节点的所有祖先.

This can be solved using the networkx library. First, build a directed graph from the DataFrame, and then find all ancestors of the leaf nodes.

import networkx as nx

leaves = set(df.child).difference(df.parent)
g = nx.from_pandas_edgelist(df, 'parent', 'child', create_using=nx.DiGraph())
ancestors = {
    n: nx.algorithms.dag.ancestors(g, n) for n in leaves
}

(pd.DataFrame.from_dict(ancestors, orient='index')
   .rename(lambda x: 'parent_{}'.format(x+1), axis=1)
   .rename_axis('child')
   .fillna(''))

      parent_1 parent_2 parent_3
child                           
h            a        c        f
g            a        c         
d            a        b         

这篇关于用pandas查找树中叶节点的所有祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆