本文介绍了如何使用索引属性转换大 pandas 数据帧的时间序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 关闭
2015-02-20 14: 00:00 1200.1
2015-02-20 14:10:00 1199.8
2015-02-21 14:00:00 1199.3
2015-02-21 14:10:00 1199.0
2015-02-22 14:00:00 1198.4
2015-02-22 14:10:00 1199.7

如何应用一个将其转换成数据框的函数,如下所示:

  '14: 00''14:10'
2015-02-20 1200.1 1199.8
2015-02-21 1199.3 1199.0
2015-02-22 1198.4 1199.7

注意:这是一个简化的例子。实际的数据框架有很多天,也是所有的日内分钟。所以如果它是一个有效的程序,这将是有用的。



谢谢

解决方案

您可以在索引的日期时间组件之间进行转换:



创建框架:

  i = pd.to_datetime(['2015-02-20 14: 00:00','2015-02-20 14:10:00','2015-02-21 14:20:00'\ 
,'2015-02-21 14:30:00' '2015-02-22 14:40:00','2015-02-22 14:50:00'])
df = pd.DataFrame(index = i,data = {'Close':[1200.1 ,1199.8,1199.3,1199.0,1198.4,1199.7]})

pivot:

  pd.pivot_table(df,index = df.index.date,columns = df.index.time,values ='Close')

返回:

  14:00:00 14:10:00 14:20:00 14:30:00 14:40:00 14:50:00 
2015-02-20 1200.1 1199.8 NaN NaN NaN NaN
2015-02-21 NaN NaN 1199。 3 1199 NaN NaN
2015-02-22 NaN NaN NaN NaN 1198.4 1199.7

use aggfunc 作为 pivot_table 的参数,以确定必要时如何汇总数据


Given a dataframe with time series that looks like this:

                      Close
2015-02-20 14:00:00  1200.1
2015-02-20 14:10:00  1199.8
2015-02-21 14:00:00  1199.3
2015-02-21 14:10:00  1199.0
2015-02-22 14:00:00  1198.4
2015-02-22 14:10:00  1199.7

How can I apply a function that transforms it into a dataframe like this:

          '14:00' '14:10'
2015-02-20 1200.1 1199.8
2015-02-21 1199.3 1199.0
2015-02-22 1198.4 1199.7

Note: This is a simplified example. The actual dataframe has many days and all the intraday minutes too. So it would be useful if it is an efficient procedure.

Thanks

解决方案

you can pivot on the date and time components of the index:

Create the frame:

i =pd.to_datetime(['2015-02-20 14:00:00','2015-02-20 14:10:00','2015-02-21 14:20:00'\
               ,'2015-02-21 14:30:00','2015-02-22 14:40:00','2015-02-22 14:50:00'])
df =pd.DataFrame(index=i, data={'Close':[1200.1,1199.8,1199.3,1199.0,1198.4,1199.7]})

pivot:

pd.pivot_table(df, index= df.index.date, columns=df.index.time, values = 'Close')

returns:

          14:00:00  14:10:00    14:20:00    14:30:00    14:40:00    14:50:00
2015-02-20  1200.1  1199.8      NaN         NaN         NaN         NaN
2015-02-21  NaN     NaN         1199.3      1199        NaN         NaN
2015-02-22  NaN     NaN         NaN         NaN         1198.4     1199.7

use aggfunc as an argument of pivot_table to determine how data is aggregated if necessary

这篇关于如何使用索引属性转换大 pandas 数据帧的时间序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:30