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.
Este exercício faz parte do curso
Intermediate Network Analysis in Python
Instruções do exercício
- Initialize an empty node list called
nodelist. - Use a
forloop to iterate over the nodes of theG_people. Inside the loop:- Update the
nodeinfodictionary using the.update()method withdas the argument. - Append the
nodeinfodictionary tonodelist.
- Update the
- Create a pandas DataFrame of the nodelist called
node_dfusing thepd.DataFrame()function.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 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())