所以我有一个数据框,看起来像
column
index1 index2
0 0 10
1 11
2 12
3 13
4 14
1 0 20
1 21
2 22
3 23
4 24我想获得一个数据帧,它看起来像
column cumulative
index1 index2
0 0 10 []
1 11 [10]
2 12 [10, 11]
3 13 [10, 11, 12]
4 14 [10, 11, 12, 13]
1 0 20 []
1 21 [20]
2 22 [20, 21]
3 23 [20, 21, 22]
4 24 [20, 21, 22, 23]我保证索引是排序的。如果我在cumulative列中存储的对象不是Python列表,而是Numpy数组,我也可以接受,如果这样可以提高实现效率的话。
基本上,目标是使用特征结果的完整历史作为某些机器学习算法中的另一个特征。如果这不是一种推荐的方法,那么请推荐其他方法。
发布于 2019-07-16 01:52:31
可能不是最优雅的:
df.assign(column=df['column'].apply(lambda x: [x])).groupby(level=0).apply(np.cumsum) column
index1 index2
0 0 [10]
1 [10, 11]
2 [10, 11, 12]
3 [10, 11, 12, 13]
4 [10, 11, 12, 13, 14]
1 0 [20]
1 [20, 21]
2 [20, 21, 22]
3 [20, 21, 22, 23]
4 [20, 21, 22, 23, 24]https://stackoverflow.com/questions/57044691
复制相似问题