Home > other >  How can I create network graph from dataframe
How can I create network graph from dataframe

Time:02-17

I have dataframe like this table below:

source destination weight
A B 0.5
A C 0.2
B C 0.1
B D 0.1
C D 0.1

How can I create network graph by source to destination and show number of weight on edge?

CodePudding user response:

You can import the data using enter image description here

CodePudding user response:

import networkx as nx
import pandas as pd

data = {'source':["A", "A", "B", "B", "C"],
        'destination':["B", "C", "C", "D", "D"],
        'weight':[0.5, 0.2, 0.1, 0.1, 0.1]}

df = pd.DataFrame(data)

g = nx.Graph()

weighted_edges = list(zip(*[df[col] for col in df]))

g.add_weighted_edges_from(weighted_edges)
  • Related