Julia DataFrame中某列的累积总和 [英] Cumulative sum of a column in Julia DataFrame

查看:117
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆