Ratios
While not strictly a measure of importance to a network, the idea of being "ratio'd" is a network measure which is particular to Twitter and is typically used to judge the unpopularity of a tweet. "The Ratio," as it is called, is calculated by taking the number of replies and dividing it by the number of retweets. For our purposes, it makes conceptual sense to take only the in-degrees of both the retweet and reply networks.
The networks G_rt
and G_reply
, and column_names = ['screen_name', 'degree']
have been loaded for you.
This is a part of the course
“Analyzing Social Media Data in Python”
Exercise instructions
- Calculate the in-degree for the retweet network with the graph method
.in_degree()
. - Do the same for the reply network.
- Merge the two DataFrames together using
.merge()
. - Calculate the ratio. The column names are
degree_reply
anddegree_rt
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Calculate in-degrees and store in DataFrame
degree_rt = pd.DataFrame(list(____.____()), columns = column_names)
degree_reply = pd.DataFrame(list(____.____()), columns = column_names)
# Merge the two DataFrames on screen name
ratio = ____.____(____, on = 'screen_name', suffixes = ('_rt', '_reply'))
# Calculate the ratio
ratio['ratio'] = ____ / ____
# Exclude any tweets with less than 5 retweets
ratio = ratio[ratio['degree_rt'] >= 5]
# Print out first five with highest ratio
print(ratio.sort_values('ratio', ascending = False).head())
This exercise is part of the course
Analyzing Social Media Data in Python
In this course, you'll learn how to collect Twitter data and analyze Twitter text, networks, and geographical origin.
Network analysis with Twitter data.
Exercise 1: Twitter networksExercise 2: Types of Twitter networksExercise 3: Which direction is the arrow?Exercise 4: Importing and visualizing Twitter networksExercise 5: Creating retweet networkExercise 6: Creating reply networkExercise 7: Visualizing retweet networkExercise 8: Individual-level network metricsExercise 9: In-degree centralityExercise 10: Betweenness centralityExercise 11: RatiosWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.