1

Trying to get the mean sentiment by day, but cannot figure it out.

I have a list of tweet sentiment values and the timestamps of the tweets. I want to get the daily mean values, but I cannot get rid of the timestamp values so my groupby is not working.

ShNBl84
  • 55
  • 1
  • 12
  • Is `Date` stored as string or as datetime ? If it's a string, convert it into datetime. You will be able to filter the df then. You can check [this question](https://stackoverflow.com/questions/32204631/how-to-convert-string-to-datetime-format-in-pandas-python) if you want to know more about coneverting string to datetime. – Gary Feb 11 '20 at 19:39
  • The Date column type is a pandas series. My goal is to have only the date values exist so I can groupby the mean value per day. – ShNBl84 Feb 11 '20 at 19:46

2 Answers2

2

You could do this:

initial df:

    Date                        Sentiment
0   2020-01-31 00:00:00+00:00   0.6369
1   2020-01-31 01:00:00+00:00   0.3612

Code:

df['Date']=pd.to_datetime(df['Date'], utc=False)
df['Date']=df['Date'].dt.date

df.groupby('Date')['Sentiment'].mean()

Output:

Date
2020-01-31    0.49905
Name: Sentiment, dtype: float64

final df:

    Date        Sentiment
0   2020-01-31  0.6369
1   2020-01-31  0.3612
Gary
  • 909
  • 8
  • 20
1

Found it: https://stackoverflow.com/a/39400136/5822871

df = df.groupby([df['Date_Time'].dt.date]).mean()

ShNBl84
  • 55
  • 1
  • 12