List of graphs
In this set of exercises, you'll use a college messaging dataset to learn how to filter graphs for time series analysis. In this dataset, nodes are students, and edges denote messages being sent from one student to another. The graph as it stands right now captures all communications at all time points.
Let's start by analyzing the graphs in which only the edges change over time.
The dataset has been loaded into a DataFrame called data
. Feel free to explore it in the IPython Shell. Specifically, check out the output of data['sender']
and data['recipient']
.
This exercise is part of the course
Intermediate Network Analysis in Python
Exercise instructions
- Initialize an empty list called
Gs
. - Use a
for
loop to iterate overmonths
. Inside the loop:- Instantiate a new undirected graph called
G
, using thenx.Graph()
function. - Add in all nodes that have ever shown up to the graph. To do this, use the
.add_nodes_from()
method onG
two times, first withdata['sender']
as argument, and then withdata['recipient']
. - Filter the DataFrame so there's only the given month. This has been done for you.
- Add edges from the filtered DataFrame. To do this, use the
.add_edges_from()
method withdf_filtered['sender']
anddf_filtered['recipient']
passed intozip()
. - Append
G
to the list of graphsGs
.
- Instantiate a new undirected graph called
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import networkx as nx
months = range(4, 11)
# Initialize an empty list: Gs
Gs = []
for month in months:
# Instantiate a new undirected graph: G
G = ____
# Add in all nodes that have ever shown up to the graph
____
____
# Filter the DataFrame so that there's only the given month
df_filtered = data[data['month'] == month]
# Add edges from filtered DataFrame
____
# Append G to the list of graphs
____
print(len(Gs))