Get startedGet started for free

Creating retweet network

Social media is, by nature, networked data. Twitter networks manifest in multiple ways. One of the most important types of networks that appear in Twitter are retweet networks. We can represent these as directed graphs, with the retweeting user as the source and the retweeted person as the target. With Twitter data in our flattened DataFrame, we can import these into networkx and create a retweet network.

For this exercise and the rest of this course we'll be using a dataset based on the 2018 State of the Union speech given by Donald Trump. Those tweets have been loaded for you in sotu_retweets.

This exercise is part of the course

Analyzing Social Media Data in Python

View Course

Exercise instructions

  • Import networkx as nx.
  • Use the user's screen name as the source argument.
  • Use the retweeted user's screen name as the target argument.
  • Ensure that the network is a directed graph in the create_using argument.

Hands-on interactive exercise

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

# Import networkx
import ____ as ____

# Create retweet network from edgelist
G_rt = nx.from_pandas_edgelist(
    sotu_retweets,
    source = ____,
    target = ____,
    create_using = ____)
 
# Print the number of nodes
print('Nodes in RT network:', len(G_rt.nodes()))

# Print the number of edges
print('Edges in RT network:', len(G_rt.edges()))
Edit and Run Code