LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Intermediate Network Analysis in Python

Kurs anzeigen

Anleitung zur Übung

  • Initialize an empty node list called nodelist.
  • Use a for loop to iterate over the nodes of the G_people. Inside the loop:
    • Update the nodeinfo dictionary using the .update() method with d as the argument.
    • Append the nodeinfo dictionary to nodelist.
  • Create a pandas DataFrame of the nodelist called node_df using the pd.DataFrame() function.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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())
Code bearbeiten und ausführen