Get startedGet started for free

In-degree centrality

Centrality is a measure of importance of a node to a network. There are many different types of centrality and each of them has slightly different meaning in Twitter networks. We are first focusing on degree centrality, since its calculation is straightforward and has an intuitive explanation.

For directed networks like Twitter, we need to be careful to distinguish between in-degree and out-degree centrality, especially in retweet networks. In-degree centrality for retweet networks signals users who are getting many retweets.

networkx has been imported as nx. Also, the networks G_rt and G_reply and column_names = ['screen_name', 'degree_centrality'] have been loaded for you.

This exercise is part of the course

Analyzing Social Media Data in Python

View Course

Exercise instructions

  • Calculate in-degree centrality for the retweet network with nx.in_degree_centrality() and store it in rt_centrality.
  • Do the same for the reply network and store it in reply_centrality.
  • Pass the items (i.e. the key-value tuples) of the reply centralities to the DataFrame constructor.
  • Do the same for the reply network.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Generate in-degree centrality for retweets 
rt_centrality = ____

# Generate in-degree centrality for replies 
reply_centrality = ____

# Store centralities in DataFrame
rt = pd.DataFrame(list(____), columns = column_names)
reply = pd.DataFrame(list(____), columns = column_names)

# Print first five results in descending order of centrality
print(rt.sort_values('degree_centrality', ascending = False).head())

# Print first five results in descending order of centrality
print(reply.sort_values('degree_centrality', ascending = False).head())
Edit and Run Code