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.
Bu egzersiz
Intermediate Network Analysis in Python
kursunun bir parçasıdırEgzersiz talimatları
- 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'].
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
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():
____