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.

2
  • 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 if you want to know more about coneverting string to datetime. Commented Feb 11, 2020 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. Commented Feb 11, 2020 at 19:46

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.