Make nodelist
You're now going to practice converting graphs to pandas
representation. If you have taken any of DataCamp's pandas courses, you will know that there is a DataFrame.to_csv('filename.csv')
method that lets you save it as a CSV file, which is a human-readable version. The main concept we hope you take away from here is the process of converting a graph to a list of records.
Start by re-familiarizing yourself with the graph data structure by calling list(G.nodes(data=True))[0]
in the IPython Shell to examine one node in the graph.
This exercise is part of the course
Intermediate Network Analysis in Python
Exercise instructions
- Initialize an empty node list called
nodelist
. - Use a
for
loop to iterate over the nodes of theG_people
. Inside the loop:- Update the
nodeinfo
dictionary using the.update()
method withd
as the argument. - Append the
nodeinfo
dictionary tonodelist
.
- Update the
- Create a pandas DataFrame of the nodelist called
node_df
using thepd.DataFrame()
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Initialize a list to store each edge as a record: nodelist
nodelist = ____
for n, d in G_people.nodes(data=True):
# nodeinfo stores one "record" of data as a dict
nodeinfo = {'person': n}
# Update the nodeinfo dictionary
____
# Append the nodeinfo to the node list
____
# Create a pandas DataFrame of the nodelist: node_df
node_df = ____
print(node_df.head())