问题描述
熊猫DataFrame
包含名为"date"
的列,该列包含非唯一的datetime
值.我可以使用以下方法在此框架中对行进行分组:
A Pandas DataFrame
contains column named "date"
that contains non-unique datetime
values.I can group the lines in this frame using:
data.groupby(data['date'])
但是,这会按datetime
值分割数据.我想按日期"列中存储的年份对这些数据进行分组. 此页面显示了在使用时间戳作为索引的情况下如何按年份分组,就我而言,这是不正确的.
However, this splits the data by the datetime
values. I would like to group these data by the year stored in the "date" column. This page shows how to group by year in cases where the time stamp is used as an index, which is not true in my case.
如何实现此分组?
推荐答案
我正在使用熊猫0.16.2.这在我的大型数据集上具有更好的性能:
I'm using pandas 0.16.2. This has better performance on my large dataset:
data.groupby(data.date.dt.year)
使用dt
选项并玩弄weekofyear
,dayofweek
等变得容易得多.
Using the dt
option and playing around with weekofyear
, dayofweek
etc. becomes far easier.
这篇关于如何在非唯一列中按日期对pandas DataFrame条目进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!