ComeçarComece de graça

Accessing user-defined location

In the slides, we saw that we could obtain user location via user-generated text, including the tweet itself and the location field in the user's description. These are the two most imprecise methods of obtaining user location, but also possibly more readily available.

In this exercise, you're going extract the user-defined location from a single example tweet as well as a large set of tweets. We've added another line to our flatten_tweets() function which will allow you to access user-defined location within the data frame.

tweet_obj['user-location'] = tweet_obj['user']['location']

In addition, the single tweet in JSON format tweet_json and the State of the Union tweets in JSON format tweets_sotu_json have been loaded for you.

Este exercício faz parte do curso

Analyzing Social Media Data in Python

Ver curso

Instruções do exercício

  • Print out the user-defined location of a single tweet in the Twitter JSON.
  • Flatten the Twitter JSON of the State of the Union tweets.
  • Print out the first 10 user-defined locations in tweets_sotu with the value_counts() and head() functions.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Print out the location of a single tweet
print(tweet_json[____][____])

# Flatten and load the SOTU tweets into a dataframe
tweets_sotu = pd.DataFrame(____(____))

# Print out top five user-defined locations
print(tweets_sotu[____].____().head())
Editar e executar o código