简体   繁体   English

按日和小时分组后重命名熊猫列

[英]Renaming Pandas Columns After Groupby Day and Hour

I have a pandas dataframe which has a datetime column. 我有一个带有datetime列的pandas数据框。 I'm grouping by day and then hour using the following: 我使用以下方法按天分组,然后按小时分组:

df.groupby([df['date'].map(lambda t: t.day), df['date'].map(lambda t: t.hour)]).count()

Unfortunately, this leaves me with a double index, both called date. 不幸的是,这给我留下了一个双重索引,两个索引都称为日期。 The first date is the day of the month, the second date is the hour, bytes is the count of items in that hour: 第一个日期是一个月中的一天,第二个日期是小时,字节是该小时中的项目计数:

在此处输入图片说明

I'm trying to utilize these date columns but can't. 我正在尝试利用这些日期列,但不能。 I've tried reseting the index, but receive this error: 我尝试重置索引,但收到此错误:

ValueError: cannot insert date, already exists

I also can't rename the columns because "date" doesn't appear in the columns list: 我也无法重命名列,因为“日期”没有出现在列列表中:

grouped_df.columns
>> Index([u'bytes'], dtype='object')

Ultimately, I'm trying to find a count of number of items in each hour of each day. 最终,我试图找到每天每一小时的商品数。 How can I rename the duplicate date columns? 如何重命名重复的日期列? Should I be grouping the dataframe using a different method to avoid this dilemma? 我是否应该使用其他方法对数据帧进行分组以避免这种困境?

我没有测试,但是类似的东西应该可以工作:

df.groupby([df['date'].rename("day").map(lambda t: t.day), df['date'].rename("hour").map(lambda t: t.hour)]).count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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