我正在寻找一种快速的方法来完成以下任务:
假设我有以下数据框:
value
index
1 'a'
2 'b'
3 'c'
4 'd'
我想将它扩展到以下数据框:
value cum_value
index
1 'a' []
2 'b' ['a']
3 'c' ['a', 'b']
4 'd' ['a', 'b', 'c']
解决我的问题的最高效方法是什么?
最佳答案
这是一种匹配输出的方法,添加一个不包含在字符串类型列中的 sep
s = (df.value+'~').shift().fillna('').cumsum().str[:-1].str.split('~')
index
1 []
2 [a]
3 [a, b]
4 [a, b, c]
Name: value, dtype: object
df['New'] = s
关于python - Pandas :要列出的列的累积值[无迭代],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57582874/