Julia DataFrame 中一列的累积和 [英] Cumulative sum of a column in Julia DataFrame
本文介绍了Julia DataFrame 中一列的累积和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Python Pandas 中,如果我想创建一个包含现有列的累积总和的新列:
In Python Pandas if I want to create a new column with the cumulative sum of an existing column I do:
df['cumulative_sum'] = df.scores.cumsum()
在 Julia 中执行此操作的等效方法是什么?
What would be the equivalent way of doing this in Julia?
推荐答案
你可以使用 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)
语法更简洁.
这篇关于Julia DataFrame 中一列的累积和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文