【问题标题】:Finding ultimate parent寻找终极父母
【发布时间】:2021-12-15 20:10:43
【问题描述】:

我正在努力寻找 Dir pandas 的终极父母。但是该任务有一个特殊之处,即图表并不真正适合,或者我根本不知道如何正确使用它。 输入:

Child Parent Class
1001 8888 A
1001 1002 D
1001 1002 C
1001 1003 C
1003 6666 G
1002 9999 H

输出:

Child Ultimate_Parent Class Connection
1001 8888 A Direct
1001 9999 D Indirect
1001 9999 C Indirect
1001 6666 C Indirect
1003 6666 G Direct
1002 9999 H Direct

我愿意:

import pandas as pd 
import networx as nx 
df = pd.DataFrame({'Child': ['1001', '1001', '1001', '1001', '1003', '1004'], 'Parent': ['8888', '1002', '1002', '1003', '6666', '9999'],'Class': ['A','D','C','C','G','H']})
    def get_hierarchy (df):
        DiG=nx.from_pandas_adgelist (df,'child','parent',create_using=nx.DiGraph())
        return pd.DataFrame.from_records([(n1,n2) for n1 in DiG.nodes() for n2 in nx.ancestors(DiG, n1)], columns=['child','Ultimate_parent'])
    df=df.toPandas()
    df=get_hierarchy(df)
    return df

我不知道如何在这里使用 Class 属性,以显示两次 1001 和 D 和 C 类。

【问题讨论】:

  • 您是否混淆了子列和父列?我本来希望一个父母有很多孩子,而不是一个孩子有很多父母。
  • df=df.toPandas() 没有意义。 df 已经是 pandas 数据框了

标签: python pandas pyspark hierarchy


【解决方案1】:

使用G.predecessors 来检测当前Parent 是否是树的根。如果是,则连接为Direct,否则连接为Indirect

G = nx.from_pandas_edgelist(df, source='Parent', target='Child',
                            create_using=nx.DiGraph)

roots = [node for node, degree in G.in_degree() if degree == 0]

ultimate_parent = [node if node in roots else list(G.predecessors(node))[0] 
                       for node in df['Parent']]

df['Ultimate_Parent'] = ultimate_parent
df['Connection'] = np.where(df['Parent'] == df['Ultimate_Parent'],
                            'Direct', 'Indirect')

输出:

>>> df
   Child  Parent Class  Ultimate_Parent Connection
0   1001    8888     A             8888     Direct
1   1001    1002     D             9999   Indirect
2   1001    1002     C             9999   Indirect
3   1001    1003     C             6666   Indirect
4   1003    6666     G             6666     Direct
5   1002    9999     H             9999     Direct

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多