0

I have a text file with data as below: A D 15 B A 11 C H 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!

3
  • You want to plot a graph or store it in a variable? Look for NetworkX library. Commented Apr 10, 2017 at 11:42
  • @Serenity I want to store it in variable Commented Apr 10, 2017 at 15:45
  • @Dadep : How is that even distantly connected to the question? Commented Apr 10, 2017 at 16:39

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.