如何将日期列级分组并替换为包含相应ISO周号的列级
我有一个具有以下列级别的数据框:
我想将日期级别分组到相应的ISO周数,并且输出等于:
如下:
df = df.groupby(
lambda d: d.isocalendar().week,
level='DATE', axis='columns'
).sum()
df.columns.rename('WK', inplace=True)
结果:
这是我想要的一部分,但是我想保留列的其他级别的多索引(在这种情况下为Year and Cat)。一年是整数,而猫可以是字符串或分类。
I have a dataframe with the following column levels:
I would like to groupby the DATE level to the corresponding iso week number, and have the output equal to:
I tried the following:
df = df.groupby(
lambda d: d.isocalendar().week,
level='DATE', axis='columns'
).sum()
df.columns.rename('WK', inplace=True)
results in:
which is part of what I want, but I would like to keep the other levels of the column MultiIndex (YEAR and CAT in this case). Where Year is an integer, and CAT could be string, or categorical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以首先重命名
日期
几周,然后按所有4个列中的多4级汇总 :
You can first rename level
DATE
for weeks and then aggregatesum
per all 4 levels ofMultiIndex in columns
: