Create a graph from the pandas DataFrame
Let's start by creating a graph from a pandas DataFrame. In this exercise, you'll create a new bipartite graph by looping over the edgelist (which is a DataFrame
object).
For simplicity's sake, in this graph construction procedure, any edge between a student and a forum node will be the 'last' edge (in time) that a student posted to a forum over the entire time span of the dataset, though there are ways to get around this.
Additionally, to shorten the runtime of the exercise, we have provided a sub-sampled version of the edge list as data
. Explore it in the IPython Shell to familiarize yourself with it.
This exercise is part of the course
Intermediate Network Analysis in Python
Exercise instructions
- Instantiate a new Graph called
G
. - Add nodes from each of the partitions. Use the
.add_nodes_from()
method to do this. The two partitions are'student'
and'forum'
. To add nodes from the'student'
partition, for example, the arguments to.add_nodes_from()
would bedata['student']
andbipartite='student'
. - Add in each edge along with the date the edge was created. To do this, use the
.add_edge()
method inside the loop, with the argumentsd['student']
,d['forum']
, anddate=d['date']
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import networkx as nx
# Instantiate a new Graph: G
G = ____
# Add nodes from each of the partitions
____
____
# Add in each edge along with the date the edge was created
for r, d in data.iterrows():
____