Last active
June 6, 2021 03:37
-
-
Save jinhangjiang/3fd7d59b8df9277544fe622a62ada659 to your computer and use it in GitHub Desktop.
Convert an edge list to a NetworkX graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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