在PANDAS中是否有一个函数可以为序列的每个点创建一个向量,该向量具有N的值-一组列的序列的先前值?
Ej:
从
pd.DataFrame([('A1', 'B1', 'C1', 'X'),
('A2', 'B2', 'C2', 'X'),
('A3', 'B3', 'C3', 'X'),
('A4', 'B4', 'C4', 'X'),
('A5', 'B5', 'C5', 'X'),
('A6', 'B6', 'C6', 'X'),
('A7', 'B7', 'C7', 'X'),
('A8', 'B8', 'C8', 'X'),
('A9', 'B9', 'C9', 'X')])
至
pd.DataFrame([(['A1', None, None], ['B1', None, None], ['C1', None, None], 'X'),
(['A2', 'A1', None], ['B2', 'B1', None], ['C2', 'C1', None], 'X'),
(['A3', 'A2', 'A1'], ['B3', 'B2', 'A1'], ['C3', 'C2', 'C1'], 'X'),
(['A4', 'A3', 'A2'], ['B4', 'B3', 'B2'], ['C4', 'C3', 'C2'], 'X'),
(['A5', 'A4', 'A3'], ['B5', 'B4', 'B3'], ['C5', 'C4', 'C3'], 'X')])
最佳答案
我认为使用Numpy的stride_tricks
库最方便
from numpy.lib.stride_tricks import as_strided as strided
v = df.values[:, :-1]
pad = np.empty((2, n1), v.dtype)
pad.fill(None)
v = np.row_stack([pad, v])
s0, s1 = v.strides
n0, n1 = v.shape
idx = df.index
col = df.columns[:-1]
pd.DataFrame(
strided(v, (n0 - 2, n1, 3), (s0, s1, s0))[:, :, ::-1].tolist(),
idx, col
).join(df.drop(col, 1))
0 1 2 3
0 [A1, None, None] [B1, None, None] [C1, None, None] X
1 [A2, A1, None] [B2, B1, None] [C2, C1, None] X
2 [A3, A2, A1] [B3, B2, B1] [C3, C2, C1] X
3 [A4, A3, A2] [B4, B3, B2] [C4, C3, C2] X
4 [A5, A4, A3] [B5, B4, B3] [C5, C4, C3] X
5 [A6, A5, A4] [B6, B5, B4] [C6, C5, C4] X
6 [A7, A6, A5] [B7, B6, B5] [C7, C6, C5] X
7 [A8, A7, A6] [B8, B7, B6] [C8, C7, C6] X
8 [A9, A8, A7] [B9, B8, B7] [C9, C8, C7] X
关于python - 将来自先前数据的信息分组到向量的数据帧中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53268527/