Search code examples
pythontwitter

Exporting tweets to a dataframe


I can't export the user information as a data frame even though it appears fine in the console when I print (users_info). Can someone help? Thanks.

# Define the search term and the date_since date as variables
search_words = "#sunrise"
date_since = "2019-09-01"

# Collect tweets
tweets = tw.Cursor(api.search,
              q=search_words,
              lang="en",
              since=date_since).items(5)

users_info = [[tweet.user.screen_name, tweet.user.location, tweet.text, tweet.created_at, tweet.retweet_count, tweet.source] for tweet in tweets]

df = pd.DataFrame(users_info, columns =['user_name','user_location', 'text', 'date', 'retweet_count', 'url'])
df.to_excel=('sunrise_tweets.xlsx')

Solution

  • There should not be a = after df.to_excel, as this causes your filename to be assigned to df.to_excel instead calling the df.to_excel method:

    df.to_excel('sunrise_tweets.xlsx')
    

    Also ensure you have installed openpyxl or XlsxWriter. See the docs for further information.