Reputation: 5496
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()
What would be the equivalent way of doing this in Julia?
Upvotes: 6
Views: 4401
Reputation: 1742
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
Per @Bogumił Kamiński's comment below, you can also do:
df.cumulative_sum = cumsum(df.scores)
which is cleaner syntax.
Upvotes: 14