首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >高效构造列表parent-child - Python/Pandas中所有后代的列表

高效构造列表parent-child - Python/Pandas中所有后代的列表
EN

Stack Overflow用户
提问于 2021-10-21 15:43:34
回答 1查看 56关注 0票数 0

我有一个包含四列的数据帧: parent_serialno、child_serialno、parent_function和child_function。我想构造一个数据帧,其中每行都是根父级,每列都是一个函数,其值是该函数的序列号。

例如,数据帧如下所示:

代码语言:javascript
复制
df = pd.DataFrame(
    [['001', '010', 'A', 'B'], ['001', '020', 'A', 'C'], ['010', '100', 'B', 'D'], ['100', '110', 'D', 'E'],
     ['002', '030', 'A', 'B'], ['002', '040', 'A', 'C']],
    columns=['parent_serialno', 'child_serialno', 'parent_function', 'child_function'])

请注意,并不是所有的函数都包含每个根的子代,但对于给定的根,每个函数只有一个序列号。根序列号是提前知道的。

我想要输出的数据帧类似于:

代码语言:javascript
复制
pd.DataFrame([['001','010','020','100','110'],['002','030','040', np.nan, np.nan]], columns = ['A','B','C','D','E'])

Out[1]: 
     A    B    C    D    E
0  001  010  020  100  110
1  002  030  040  NaN  NaN

This post展示了如何获得字典层次结构,但我不太关心识别树叶在树中的位置(即孙子和曾孙),而更关心的是识别每个叶子的根和功能。

EN

回答 1

Stack Overflow用户

发布于 2021-10-21 17:48:12

使用networkx解决此问题:

代码语言:javascript
复制
# Python env: pip install networkx
# Anaconda env: conda install networkx

# Create a list of tuples of serialno / function
df['parent'] = df[['parent_function', 'parent_serialno']].apply(tuple, axis=1)
df['child'] = df[['child_function', 'child_serialno']].apply(tuple, axis=1)

# Create a directed graph from dataframe
G = nx.from_pandas_edgelist(df, source='parent', target='child', 
                            create_using=nx.DiGraph)

# Find roots and leaves
roots = [node for node, degree in G.in_degree() if degree == 0]
leaves = [node for node, degree in G.out_degree() if degree == 0]

# Find all path from each root to each leaf
paths = {}
for root in roots:
    children = paths.setdefault(root, [])
    for leaf in leaves:
        for path in nx.all_simple_paths(G, root, leaf):
            children.extend(path[1:])
    children.sort(key=lambda x: x[1])

# Create your final output
out = pd.DataFrame([dict([parent] + children) for parent, children in paths.items()])

输出:

代码语言:javascript
复制
>>> out
     A    B    C    D    E
0  001  010  020  100  110
1  002  030  040  NaN  NaN
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69664966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档