I have a text file with data as below: AD 15 BA 11 CH 2 . . . . . .
AD 15 BA 11 CH 2 . . . . . .
I read data using Dataframe in Python. Then I want to create a graph with vertices in columns 1 & 2 and column 3 is the weight.
How can I create a graph from the data? Thank you!
Here's a quick example using 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.