[英]Group all elements of Pandas Dataframe by Datetime column
Imagine a Dataframe with 3 columns: 想象一下一个包含3列的数据框:
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: 您可以将
groupby
与以下内容一起使用:
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. df
是您的数据框。 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')
如果
groupby(by='Date')
天分组,则用要分组的日期创建另一个列Day
,然后用groupby(by='Day')
替换groupby(by='Date')
groupby(by='Day')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.