Search code examples
pythonpandasapicsvtwitter

Updating a pandas dataframe that exports Twitter Data to CSV


I am currently exploring the Twitter and wanted to save my results to a csv file. I used:

#create dataframe
columns = ['source', 'created_at', 'ID', 'user', 'tweet', 'likes', 'location', 'coordinates']
data = []

for tweet in tweets:
    data.append([tweet.source, tweet.created_at, tweet.id, tweet.user.screen_name, tweet.full_text, tweet.favorite_count, tweet.user.location, tweet.coordinates])

df = pd.DataFrame(data, columns=columns)

df.to_csv('tweets.csv', index=True)

to make some appropriate columns and it works really well! Now I want to keep adding to this csv, but everytime I run the code it remakes the file instead of adding /to/ it. Is there a way I can make it so it would update the file instead of replacing it?


Solution

  • You can open your file in append mode, then write to the open file:

    with open('tweets.csv', 'a') as f:
        df.to_csv(f, index=True)