Updating a pandas dataframe that exports Twitter Data to CSV

Updating a pandas dataframe that exports Twitter Data to CSV

Problem Description:

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 – 1

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)
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject