图形工具 - 读取来自 pandas 数据框的边缘列表 [英] graph-tool - reading edge lists from pandas dataframe
问题描述
我正在开始使用图形工具,从熊猫数据框 df
导入边缘列表,如:
node1 node2
0 1 2
1 2 3
2 1 4
3 3 1
4 4 3
5 1 5
所以基本上是一个有向边的列表。根据教程将它们导入到图形工具中:
from graph_tool.all import *
import pandas as pd
#阅读熊猫数据框
df = pd.read_csv('file.csv')
#定义图
g =图(定向=真)
#添加边缘
g.add_edge_list(df.values)
根据文档add_edge_list edge_list ):
edge_list 可以是形状(E,2)的状态,其中E是边的数量,每行指定一个(源,目标)对。
运行上面的代码设置 edge_list = df.values,然后绘制图形,得到:
这不是原始的 edge_list 的数据框。我尝试着设置 * edge_list * = df.values.tolist()
:
g.add_edge_list(df.values.tolist())
取得:
其中正确的是。任何人都可以重现这一点?这里的问题是我正在处理巨大的网络(〜4 * 10 ^ 6个节点),我认为 .tolist()
方法会浪费一个
编辑:添加绘制图形的代码:
code> graph_draw(g,vertex_text = g.vertex_index,vertex_font_size = 18,output_size =(200,200),output =graph.png)
我无法重现此操作。如果我从csv文件加载数据框:
node1,node2
1,2
2 ,3
1,4
3,1
4,3
1,5
在调用 g.add_edge_list(df.values)
后获得第二个数字。
I'm starting working with graph-tool, importing a list of edges from a pandas dataframe df
like:
node1 node2
0 1 2
1 2 3
2 1 4
3 3 1
4 4 3
5 1 5
So basically a list of directed edges. I'm importing them into graph-tool according to the tutorial with:
from graph_tool.all import *
import pandas as pd
# Read pandas dataframe
df = pd.read_csv('file.csv')
# Define Graph
g = Graph(directed=True)
# Add Edges
g.add_edge_list(df.values)
According to the Documentation of add_edge_list(edge_list): edge_list may be a ndarray of shape (E,2), where E is the number of edges, and each line specifies a (source, target) pair.
Running the above code setting edge_list = df.values, and drawing the graph, I obtained:
which is not a representation of the original edge_list of the dataframe. I tried to set *edge_list* = df.values.tolist()
with:
g.add_edge_list(df.values.tolist())
obtaining:
Which actually is the right one. Anyone can reproduce this? The problem here is that I'm working with huge networks (~4*10^6 nodes), and I think that the .tolist()
method is going to waste a lot of memory in the process.
EDIT: add code for drawing the graph:
graph_draw(g, vertex_text=g.vertex_index, vertex_font_size=18, output_size=(200, 200), output="graph.png")
I can't reproduce this. If I load the data frame from the csv file:
node1,node2
1,2
2,3
1,4
3,1
4,3
1,5
I get your second figure after calling g.add_edge_list(df.values)
.
这篇关于图形工具 - 读取来自 pandas 数据框的边缘列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!