Get startedGet started for free

Make edgelist

Now, you're going to apply the same ideas to making an edge list. Go forth and give it a shot!

As with the previous exercise, run list(G.edges(data=True))[0] in the IPython Shell to get a feel for the edge list data structure before proceeding.

This exercise is part of the course

Intermediate Network Analysis in Python

View Course

Exercise instructions

  • Initialize a list called edgelist to store each edge as a record.
  • Use a for loop to iterate over the edges of G_people. Inside the loop:
    • Initialize a dictionary called edgeinfo that shows edge information.
    • Update the edgeinfo dictionary with the metadata dictionary d.
    • Append the edgeinfo dictionary to edgelist.
  • Create a pandas DataFrame of the edgelist called edge_df.

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: edgelist
edgelist = []
for n1, n2, d in G_people.edges(data=True):
    # Initialize a dictionary that shows edge information: edgeinfo
    edgeinfo = {'node1':____, 'node2':____}
    
    # Update the edgeinfo data with the edge metadata
    ____
    
    # Append the edgeinfo to the edgelist
    ____
    
# Create a pandas DataFrame of the edgelist: edge_df
edge_df = ____
print(edge_df.head())
Edit and Run Code