Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫将时间序列转换为多列数据帧_Python_Pandas_Dataframe_Pandas Groupby - Fatal编程技术网

Python 熊猫将时间序列转换为多列数据帧

Python 熊猫将时间序列转换为多列数据帧,python,pandas,dataframe,pandas-groupby,Python,Pandas,Dataframe,Pandas Groupby,我有一系列的日内数据,如下所示 ts =pd.Series(np.random.randn(60),index=pd.date_range('1/1/2000',periods=60, freq='2h')) 我希望将数据转换成一个数据框,列作为每个日期,行作为日期中的时间 我已经试过了 key = lambda x:x.date() grouped = ts.groupby(key) 但是如何将这些组转换为日期列数据帧呢?还是有更好的办法 import pandas as pd impor

我有一系列的日内数据,如下所示

ts =pd.Series(np.random.randn(60),index=pd.date_range('1/1/2000',periods=60, freq='2h'))
我希望将数据转换成一个数据框,列作为每个日期,行作为日期中的时间

我已经试过了

key = lambda x:x.date()
grouped = ts.groupby(key)
但是如何将这些组转换为日期列数据帧呢?还是有更好的办法

import pandas as pd
import numpy as np

index = pd.date_range('1/1/2000', periods=60, freq='2h')
ts = pd.Series(np.random.randn(60), index = index)

key = lambda x: x.time()
groups = ts.groupby(key)

print pd.DataFrame({k:g for k,g in groups}).resample('D').T
输出:

          2000-01-01  2000-01-02  2000-01-03  2000-01-04  2000-01-05  2000-01-06  \
00:00:00    0.109959   -0.124291   -0.137365    0.054729   -1.305821   -1.928468   
03:00:00    1.336467    0.874296    0.153490   -2.410259    0.906950    1.860385   
06:00:00   -1.172638   -0.410272   -0.800962    0.568965   -0.270307   -2.046119   
09:00:00   -0.707423    1.614732    0.779645   -0.571251    0.839890    0.435928   
12:00:00    0.865577   -0.076702   -0.966020    0.589074    0.326276   -2.265566   
15:00:00    1.845865   -1.421269   -0.141785    0.433011   -0.063286    0.129706   
18:00:00   -0.054569    0.277901    0.383375   -0.546495   -0.644141   -0.207479   
21:00:00    1.056536    0.031187   -1.667686   -0.270580   -0.678205    0.750386   

          2000-01-07  2000-01-08  
00:00:00   -0.657398   -0.630487  
03:00:00    2.205280   -0.371830  
06:00:00   -0.073235    0.208831  
09:00:00    1.720097   -0.312353  
12:00:00   -0.774391         NaN  
15:00:00    0.607250         NaN  
18:00:00    1.379823         NaN  
21:00:00    0.959811         NaN