更新时间:2023-11-18 18:40:10
您可以使用Base方法cumsum
计算向量的累积和,然后将其存储在数据帧的新列中:
You can use the Base method cumsum
to calculate the cumulative sum of a vector, and then store that in a new column of the dataframe:
df[!, :cumulative_sum] = cumsum(df[!, :scores]) # the ! is to avoid copying
根据下面的@BogumiłKamiński的评论,您也可以这样做:
Per @Bogumił Kamiński's comment below, you can also do:
df.cumulative_sum = cumsum(df.scores)
这是更简洁的语法.