python - 如何从 Python 中的 pandas 数据框中获取 networkx 图的分支作为列表?

标签 python python-3.x pandas list dataframe

我有一个 pandas 数据框 df 如下所示:

From    To
0   Node1   Node2
1   Node1   Node3
2   Node2   Node4
3   Node2   Node5
4   Node3   Node6
5   Node3   Node7
6   Node4   Node8
7   Node5   Node9
8   Node6   Node10
9   Node7   Node11

df.to_dict() 是:

{'From': {0: 'Node1',
  1: 'Node1',
  2: 'Node2',
  3: 'Node2',
  4: 'Node3',
  5: 'Node3',
  6: 'Node4',
  7: 'Node5',
  8: 'Node6',
  9: 'Node7'},
 'To': {0: 'Node2',
  1: 'Node3',
  2: 'Node4',
  3: 'Node5',
  4: 'Node6',
  5: 'Node7',
  6: 'Node8',
  7: 'Node9',
  8: 'Node10',
  9: 'Node11'}}

我使用 networkx 包将这个 pandas 数据框绘制为网络图,如下所示: enter image description here

我想从此网络图中获取独特场景/分支的列表。 这里有 4 个分支,从 Node1 开始。

Node1-Node2-Node4-Node8
Node1-Node2-Node5-Node9
Node1-Node3-Node6-Node10
Node1-Node3-Node7-Node11

如何从 Python 中给定的 pandas 数据框中获取上面的分支列表?

最佳答案

可以定义Recursive Function并保存路径和打印路径:

df = pd.DataFrame({
          'From':['Node1','Node1', 'Node2', 'Node2', 'Node3', 'Node3', 'Node4', 'Node5', 'Node6', 'Node7'],
          'TO'  :['Node2','Node3', 'Node4', 'Node5', 'Node6', 'Node7', 'Node8', 'Node9', 'Node10', 'Node11']
        })

def prntPath(lst, node, df, lst_vst):
    for val in df.values:
        if val[0] == node:
            lst.append(val[1])
            prntPath(lst, val[1], df, lst_vst)
    
    if not lst[-1] in lst_vst:
        print('-'.join(lst))
    for l in lst: lst_vst.add(l)
    lst.pop()
    return
    
lst_vst = set()
prntPath(['Node1'],'Node1', df, lst_vst)

输出:

Node1-Node2-Node4-Node8
Node1-Node2-Node5-Node9
Node1-Node3-Node6-Node10
Node1-Node3-Node7-Node11

您可以检查并使用其他图表,如下所示:

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from itertools import chain
from networkx.drawing.nx_pydot import graphviz_layout

def prntPath(lst, node, df, lst_vst):
    for val in df.values:
        if val[0] == node:
            lst.append(val[1])
            prntPath(lst, val[1], df, lst_vst)
    if not lst[-1] in lst_vst: print('-'.join(lst))
    for l in lst: lst_vst.add(l)
    lst.pop()
    return

df = pd.DataFrame({
          'From':['Node1','Node1', 'Node2', 'Node3', 'Node3', 'Node5', 'Node7'],
          'TO'  :['Node2','Node3', 'Node5', 'Node6', 'Node7', 'Node9', 'Node11']
        })

g = nx.DiGraph()
g.add_nodes_from(set(chain.from_iterable(df.values)))
for edg in df.values:
    g.add_edge(*edg)
pos = graphviz_layout(g, prog="dot")
nx.draw(g, pos,with_labels=True, node_shape='s')
plt.draw()
plt.show() 

lst_vst = set()
prntPath(['Node1'],'Node1', df, lst_vst)

输出:

enter image description here

Node1-Node2-Node5-Node9
Node1-Node3-Node6
Node1-Node3-Node7-Node11

关于python - 如何从 Python 中的 pandas 数据框中获取 networkx 图的分支作为列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69595274/

相关文章:

python - 在 Python 中求解 ODE 时,如何获得比 linspace 更多的变量值? (编辑)

python - 如何在执行前通过 python 程序显式配置命令提示符?

Python:如何调用子类的重写方法

python - 检查日期是否属于 pandas 数据框

python - 映射 Pandas 数据框中的值范围

python - 我无法在 ubuntu 上运行 conda 命令/激活环境?

python - 认证用户Django取决于用户

python - 在主循环外广播加速矢量化 numpy 操作?

python - 如果值为列表,则按字典中的值排序

Python,pandas,匹配组新列中的累积和