首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pandas.DataFrame.resample内部层次的MultiIndex

Pandas.DataFrame.resample内部层次的MultiIndex
EN

Stack Overflow用户
提问于 2019-09-11 14:18:28
回答 1查看 265关注 0票数 2

我需要重新采样由两个级别组成的潘达斯MultiIndex。内部级别是日期时间索引。需要重放。

代码语言:javascript
复制
import numpy as np
import pandas as pd

rng = pd.date_range('2019-01-01', '2019-04-27', freq='B', name='date')
df = pd.DataFrame(np.random.randint(0, 100, (len(rng), 2)), index=rng, columns=['sec1', 'sec2'])

df['month'] = df.index.month
df.set_index(['month', rng], inplace=True)
print(df)

# At that point I need to apply pd.resample. I'm wondering how to specify the level that I would like to resample?
df = df.resample('M').last()  # is not working;
# I'm looking for somthing like this: df = df.resample('M', level=1).last()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-11 14:28:23

尝试:

代码语言:javascript
复制
 df.groupby('month').resample('M', level=1).last()

输出:

代码语言:javascript
复制
                  sec1  sec2
month date                  
1     2019-01-31    59    87
2     2019-02-28    70    33
3     2019-03-31    71    38
4     2019-04-30    56    79

详细信息。

首先,在索引的“月份”或level=0上对数据进行分组。

接下来,在 parameter for MultiIndex中使用重采样。级别参数可以使用str、索引级别名称(在本例中为“date”),也可以使用级别编号。

最后,提出了链和聚类功能,如last

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57891210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档