Skip to content

Instantly share code, notes, and snippets.

@jinhangjiang
Last active June 6, 2021 03:37
Show Gist options
  • Save jinhangjiang/3fd7d59b8df9277544fe622a62ada659 to your computer and use it in GitHub Desktop.
Save jinhangjiang/3fd7d59b8df9277544fe622a62ada659 to your computer and use it in GitHub Desktop.
Convert an edge list to a NetworkX graph
import networkx as nx
# "edgelist" will be prepared with 3 columns: Source, Target, and Weights
# "name" give your graph a name since we are going to do graphs comparison
def convert_graph(edgelist,name):
graph=nx.Graph() # this only helps you generate a undirected graph
graph.add_weighted_edges_from([tuple(x) for x in edgelist.values]) # add weights to the edges
graph.name = "Network Graph for" + " " + name # name the graph
print(nx.info(graph)) # this will give you the basic info about the graph
print("------------------------------------")
print("************************************")
print("------------------------------------")
return graph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment