M.E.
M.E.

Reputation: 5496

Cumulative sum of a column in Julia DataFrame

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

Answers (1)

Anshul Singhvi
Anshul Singhvi

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

Related Questions