Creating a graph from an edge list
We are going to be using this file as our original edge list: https://raw.githubusercontent.com/itsgorain/datasets/main/networks/alice/edgelist_alice_original.csv. Let’s take a look:
- Before we can create our graph, we must import the two libraries we will be working with:
pandas
andnetworkx
. We usepandas
to read the edge list into a DataFrame, and we pass that DataFrame tonetworkx
to create a graph. You can import both like so:import pandas as pd
import networkx as nx
- With the libraries imported, let’s use pandas to read the CSV file into a DataFrame and then display it, as shown in the following code block:
data = 'https://raw.githubusercontent.com/itsgorain/datasets/main/networks/alice/edgelist_alice_original.csv'
network_df = pd.read_csv(data)
network_df.head()
If you run this in a Jupyter notebook, you should see the following DataFrame:
Figure 6.1 – pandas DataFrame of...