1
votes

I have an empty dataframe and I am appending one data at a time to it. Like this:

# Initialize an empty dataframe to store the tweet id and sentiment
tweets = pd.DataFrame(columns=['tweet_id', 'sentiment'])

tweet_id is an integer, sentiment is a string which can have 3 values viz. "Positive", "Negative", "Neutral". Now I have a loop which appends to the dataframe:

for i in range(len(whatever_I_want)):
    tweet = get_new_tweet()
    tweets = tweets.append({'tweet_id': tweet['id'], 'sentiment': tweet['sentiment']}, ignore_index=True)
    # Update the plot with the new dataframe
    tweets.groupby('sentiment').count()['tweet_id'].plot.bar()
    plt.show()

This is working but it is creating multiple plots and I need to close one in order to view another. I want the same plot to update whenever I run the loop. How should I do this? I searched for it, and I got solutions which are either using numpy to append or showing line charts.

How do I achieve this by using pandas groupby()?