简体   繁体   中英

Group all elements of Pandas Dataframe by Datetime column

Imagine a Dataframe with 3 columns:

index
A: datetime
B: value 1 or 2

There could be more rows for a specific day. I want to make a new dataframe which summarize the value for each day. So:

index
A: datetime (1 day)
B: amount of rows which contained value 1 in first dataframe
C: amount of rows which contained value 2 in first dataframe

Sample data:

在此处输入图片说明

You could use groupby with something like:

df['count_sentiment'] = df['Sentiment'] == 'positive' # equal to 1 iff the row is positive
df[['Date', 'Likes', 'Rts', 'count_sentiment']].groupby(by='Date').sum()

where df is your dataframe. This will group by Date. If you want to group by day, create another column Day with the day you want to group with and replace groupby(by='Date') by groupby(by='Day')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM