简体   繁体   English

从数据文件构造图

[英]Construct graph from data file

I have a text file with data as below: AD 15 BA 11 CH 2 . . . . . . 我有一个文本文件,其数据如下: AD 15 BA 11 CH 2 . . . . . . AD 15 BA 11 CH 2 . . . . . .

I read data using Dataframe in Python. 我在Python中使用Dataframe读取数据。 Then I want to create a graph with vertices in columns 1 & 2 and column 3 is the weight. 然后我要创建一个在第1列和第2列中具有顶点的图形,第3列是权重。

How can I create a graph from the data? 如何从数据创建图形? Thank you! 谢谢!

Here's a quick example using networkx : 这是一个使用networkx的简单示例:

import networkx as nx

node_list = list(set(list(df['col1']) + list(df['col2'])))  
data = [tuple(x) for x in df.values.tolist()]
# [('A', 'D', 15), ('B', 'A', 11), ('C', 'H', 2), . . .]

G = nx.Graph()
G.add_nodes_from(node_list)
G.add_weighted_edges_from(data)

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

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