基本上,当列是多索引时,pandas.DataFrame.shift不起作用:
鉴于这些值和当前设置:
idx = ['2018-03-14T06:15:39.000000000', '2018-03-14T06:16:15.000000000',
'2018-03-14T06:16:50.000000000', '2018-03-14T06:17:47.000000000',
'2018-03-14T06:18:46.000000000']
vals = [[9.15390039e+03, 9.99999978e-03, 1.64927383e+04, 4.00000000e+00,
1.00000000e+00, 0.00000000e+00, 9.15388965e+03, 9.99999978e-03,
1.64928926e+04, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00,
9.15388965e+03],
[9.15390039e+03, 9.99999978e-03, 1.64847031e+04, 9.00000000e+00,
1.00000000e+00, 0.00000000e+00, 9.15388965e+03, 9.99999978e-03,
1.64848359e+04, 3.00000000e+00, 0.00000000e+00, 1.00000000e+00,
9.15388965e+03],
[9.15999023e+03, 9.99999978e-03, 1.64850938e+04, 7.00000000e+00,
0.00000000e+00, 1.00000000e+00, 9.16000000e+03, 9.99999978e-03,
1.64851660e+04, 2.00000000e+00, 1.00000000e+00, 0.00000000e+00,
9.16000000e+03],
[9.16424023e+03, 9.99999978e-03, 1.64821777e+04, 2.20000000e+01,
0.00000000e+00, 1.00000000e+00, 9.16425000e+03, 9.99999978e-03,
1.64848125e+04, 2.30000000e+01, 1.00000000e+00, 0.00000000e+00,
9.16425000e+03],
[9.16425000e+03, 9.99999978e-03, 1.64847891e+04, 1.00000000e+01,
1.00000000e+00, 0.00000000e+00, 9.16424023e+03, 9.99999978e-03,
1.64849219e+04, 1.20000000e+01, 0.00000000e+00, 1.00000000e+00,
9.16424023e+03]]
cols = [('t_2', 'price'),
('t_2', 'spread'),
('t_2', 'volume_24h'),
('t_2', 'time_diff'),
('t_2', 'buy'),
('t_2', 'sell'),
('t_1', 'price'),
('t_1', 'spread'),
('t_1', 'volume_24h'),
('t_1', 'time_diff'),
('t_1', 'buy'),
('t_1', 'sell'),
('t_0', 'target')]
df = pandas.DataFrame(vals, index=idx,
columns=pandas.MultiIndex.from_tuples(cols))
df['t_0']['target'] = df['t_0']['target'].shift(-1)
df.head()
返回完全相同的数据帧,并且永远不会发生转换.一段时间以来,我一直在摸不着头脑,但却无法理解.
我错过了一些完全明显的东西吗
最佳答案 您正在寻找
df[('t_0', 'target')] = df[('t_0', 'target')].shift(-1)
df[('t_0', 'target')]
2018-03-14T06:15:39.000000000 9153.88965
2018-03-14T06:16:15.000000000 9160.00000
2018-03-14T06:16:50.000000000 9164.25000
2018-03-14T06:17:47.000000000 9164.24023
2018-03-14T06:18:46.000000000 NaN
Name: (t_0, target), dtype: float64
请注意,当您单独索引两次时,您将修改副本,而不是原始副本.