Pandas Dataframe从切片时间序列制作数据帧

时间:2016-04-05 09:22:48

标签: python pandas

pieces_v = [[sensor_values[i: i + 6]] for i in xrange(4)]

所以pieces_v看起来像这样:

[[0   -60.244999
1   -51.290001
2   -52.727001
3   -60.962002
4   -51.230000
5   -58.717999
Name: -64.251, dtype: float32], [1   -51.290001
2   -52.727001
3   -60.962002
4   -51.230000
5   -58.717999
6   -62.112999
Name: -64.251, dtype: float32], [2   -52.727001
3   -60.962002
4   -51.230000
5   -58.717999
6   -62.112999
7   -64.460999
Name: -64.251, dtype: float32], [3   -60.962002
4   -51.230000
5   -58.717999
6   -62.112999
7   -64.460999
8   -44.680000
Name: -64.251, dtype: float32]]

现在我想构建这个数据的连接数据框,但正如您所看到的,我的代码存在一个小问题,导致没有列标题。什么能解决这个问题?

for y in xrange(7):
    df = pd.DataFrame({ y : pd.Series(sensor_values[i:i+6] for i in xrange(7)) })

(我想尝试构建'字典数据帧'不是继续进行的方式吗?)

1 个答案:

答案 0 :(得分:0)

我认为您可以使用concat

print pd.concat([pd.Series(sensor_values[i: i + 6].values) for i in xrange(4)], axis=1)

           0          1          2          3
0 -60.244999 -51.290001 -52.727001 -60.962002
1 -51.290001 -52.727001 -60.962002 -51.230000
2 -52.727001 -60.962002 -51.230000 -58.717999
3 -60.962002 -51.230000 -58.717999 -62.112999
4 -51.230000 -58.717999 -62.112999 -64.460999
5 -58.717999 -62.112999 -64.460999 -44.680000

print pd.concat([pd.Series(sensor_values[i: i + 6].values) for i in xrange(4)], 
                axis=1, 
                keys=['a','b','c','d'])

           a          b          c          d
0 -60.244999 -51.290001 -52.727001 -60.962002
1 -51.290001 -52.727001 -60.962002 -51.230000
2 -52.727001 -60.962002 -51.230000 -58.717999
3 -60.962002 -51.230000 -58.717999 -62.112999
4 -51.230000 -58.717999 -62.112999 -64.460999
5 -58.717999 -62.112999 -64.460999 -44.680000

或者:

print pd.concat([pd.Series(sensor_values[i: i + 6]) for i in xrange(4)], 
                 axis=1, 
                 keys=['a','b','c','d'])

           a          b          c          d
0 -60.244999        NaN        NaN        NaN
1 -51.290001 -51.290001        NaN        NaN
2 -52.727001 -52.727001 -52.727001        NaN
3 -60.962002 -60.962002 -60.962002 -60.962002
4 -51.230000 -51.230000 -51.230000 -51.230000
5 -58.717999 -58.717999 -58.717999 -58.717999
6        NaN -62.112999 -62.112999 -62.112999
7        NaN        NaN -64.460999 -64.460999
8        NaN        NaN        NaN -44.680000