samsmith
samsmith

Reputation: 1

Iterate over DateTimeIndex to get overall sentiment per day

I am trying to iterate a pandas dataframe, where I have dateTimeIndex as my index, twitter text and sentiment for each tweet added (so three columns). I'm new to python and looking to find the overall sentiment per day (so there will only be one day per row, with the overall sentiment. Would I be right in using 'groupby' here. Is there an efficient way this can be done?

Upvotes: 0

Views: 138

Answers (1)

Yuke
Yuke

Reputation: 51

Yes you can use groupby followed by the appropriate function to get the overall sentiment. For example if you want to get the sum of the sentiment for each day your python would be as follows:

df.groupby('dateTimeIndex')['sentiment'].sum()

This is assuming that your dateTimeIndex contains only the date (example: 2019-07-23) and not the time (example: 2019-07-23 10:00).

If your dateTimeIndex contains both the date and time you can use dt.date function to group by date.

df.groupby(df['dateTimeIndex'].dt.date)['sentiment'].sum()

Upvotes: 1

Related Questions