简体   繁体   English

如何从 psycopg2 查询结果有效地创建 networkx 图?

[英]How can I efficiently create a networkx graph from psycopg2 query results?

I'm building a networkx graph using data retrieved from a Postgres DB using psycopg2.我正在使用使用 psycopg2 从 Postgres 数据库检索的数据构建 networkx 图。 I can create the graph just fine, but I'm wondering if there is a more efficient and/or Pythonic way to do it.我可以很好地创建图形,但我想知道是否有更有效和/或 Pythonic 的方法来做到这一点。

Current code:当前代码:

DG = nx.DiGraph()
cur.execute(edgeQuery)
for row in cur:
    self.DG.add_edge(
        row[0],            # fnode
        row[1],            # tnode
        weight=row[3],     # cost
        name=row[4]
    )

The networkx documentation indicates multiple edges can be created at once by feeding in an ebunch . networkx 文档表明可以通过输入ebunch一次创建多个边。 I could create an ebunch by iterating the cursor results, but that wouldn't be any more efficient than my current solution.我可以通过迭代游标结果来创建一个 ebunch,但这不会比我当前的解决方案更有效。 I feel like there's got to be a more efficient way to translate my cursor results into networkx edges.我觉得必须有一种更有效的方法将我的光标结果转换为 networkx 边缘。 Maybe something like zip?也许像 zip 之类的东西? I'd also like to identify a pythonic way to do it for ease of future maintenance (and to satisfy my own curiosity).我还想确定一种 pythonic 方式来做这件事,以方便将来的维护(并满足我自己的好奇心)。

Assuming I understand the data-structure of your "cur" correctly: You should be able to convert it to an array and then slice for a compact notation.假设我正确理解了“cur”的数据结构:您应该能够将其转换为数组,然后切片以获得紧凑的表示法。 For example:例如:

import numpy as np
import networkx as nx

graph = nx.DiGraph()
x= np.array([[1,2,0.5,"o"],[3,4,0.2,"a"]])
graph.add_edges_from(x[:,:2],weight = x[:,2], name = x[:,3])

the add_edges_from() also saves you the for-loop "trouble" :) Like this, the edges are now: add_edges_from() 还为您节省了 for 循环的“麻烦”:) 像这样,边缘现在是:

print(graph.edges)

[('1', '2'), ('3', '4')] [('1', '2'), ('3', '4')]

I hope this is what you're looking for.我希望这就是你要找的。

There is a much simpler way using Pandas.使用 Pandas 有一种更简单的方法。

import pandas as pd
import networkx as nx
import psycopg2

*** define psql variables***
con = psycopg2.connect(host=pg_host, port=pg_port, dbname=dbase, user=user, password=pw,options=f"-c search_path={schema}")

sql = "select a,b from nodetable"


df = pd.read_sql(sql,con)

G = nx.Graph()
G = nx.from_pandas_edgelist(df, 'a', 'b')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM