Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cumulative sum of a column in Julia DataFrame

Tags:

julia

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?

like image 513
M.E. Avatar asked Sep 21 '19 23:09

M.E.


People also ask

How do you get the cumulative sum of a column in pandas?

The cumsum() method returns a DataFrame with the cumulative sum for each row. The cumsum() method goes through the values in the DataFrame, from the top, row by row, adding the values with the value from the previous row, ending up with a DataFrame where the last row contains the sum of all values for each column.

How do you calculate cumulative percentage in pandas?

First, create a data frame as 'data_frame' and provide the values you need to calculate the cumulative sum, then pass the 'data_frame' parameter to pd. DataFrame() while specifying the column values, and finally, use the cumsum() and sum() built-in functions to calculate the cumulative percentage.

How do you find the sum of a cumulative column in R?

cumsum() function takes up column name as argument which computes the cumulative sum of the column and it is passed to rev() function which reverses the cumulative sum as shown below.


1 Answers

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.

like image 177
Anshul Singhvi Avatar answered Sep 19 '22 23:09

Anshul Singhvi