如何在非唯一列中按日期对pandas DataFrame条目进行分组 [英] How to group pandas DataFrame entries by date in a non-unique column

查看:64
本文介绍了如何在非唯一列中按日期对pandas DataFrame条目进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熊猫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选项并玩弄weekofyeardayofweek等变得容易得多.

Using the dt option and playing around with weekofyear, dayofweek etc. becomes far easier.

这篇关于如何在非唯一列中按日期对pandas DataFrame条目进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆