tseries对象转换为DataFrame

tseries对象转换为DataFrame

本文介绍了将Pandas tseries对象转换为DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将以下<'pandas.tseries.resample.DatetimeIndexResampler'> 类型对象转换为大熊猫DataFrame对象(< ;'pandas.core.frame.DataFrame'> )。然而,我在大熊猫文档中找不到相关的功能,让我这样做。

I wish to convert the following <'pandas.tseries.resample.DatetimeIndexResampler'> type object into a pandas DataFrame object (<'pandas.core.frame.DataFrame'>). However I cannot find the relevant function in the pandas documentation to allow me to do this.

数据采用以下格式:

                  M30
Date
2016-02-29  -61.187699
2016-03-31  -60.869565
2016-04-30  -61.717922
2016-05-31  -61.823966
2016-06-30  -62.142100
...

任何人都可以提供替代解决方案?

Can anyone provide an alternative solution?

推荐答案

您需要一些总体功能,如 sum 意味着

You need some aggregate function like sum or mean.

您的数据示例:

print (df)
                  M30
Date
2016-02-29 -61.187699
2016-03-31 -60.869565
2016-04-30 -61.717922
2016-05-31 -61.823966
2016-06-30 -62.142100

#resample by 2 months
r = df.resample('2M')
print (r)
DatetimeIndexResampler [freq=<2 * MonthEnds>,
                        axis=0,
                        closed=right,
                        label=right,
                        convention=start,
                        base=0]





#aggregate sum
print (r.sum())
                   M30
Date
2016-02-29  -61.187699
2016-04-30 -122.587487
2016-06-30 -123.966066

#aggregate mean
print (r.mean())
                  M30
Date
2016-02-29 -61.187699
2016-04-30 -61.293743
2016-06-30 -61.983033

#aggregate first
print (r.first())
                  M30
Date
2016-02-29 -61.187699
2016-04-30 -60.869565
2016-06-30 -61.823966

这篇关于将Pandas tseries对象转换为DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 15:02