如何在非唯一列中按日期对pandas DataFrame条目进行分组

编程入门 行业动态 更新时间:2024-10-27 21:22:57
本文介绍了如何在非唯一列中按日期对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选项并玩弄weekofyear,dayofweek等变得容易得多.

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

更多推荐

如何在非唯一列中按日期对pandas DataFrame条目进行分组

本文发布于:2023-10-26 12:51:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   按日   如何在   pandas   DataFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!