pandas 在数据框 Python 中创建累积频率列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38891974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:47:29  来源:igfitidea点击:

Creating a Cumulative Frequency Column in a Dataframe Python

pythonpandasdataframe

提问by Fxs7576

I am trying to create a new column named 'Cumulative Frequency' in a data frame where it consists of all the previous frequencies to the frequency for the current row as shown here.

我正在尝试在数据框中创建一个名为“累积频率”的新列,其中包含所有以前的频率到当前行的频率,如此处所示。

enter image description here

在此处输入图片说明

What is the way to do this?

有什么方法可以做到这一点?

回答by EdChum

You want cumsum:

你想要cumsum

df['Cumulative Frequency'] = df['Frequency'].cumsum()

Example:

例子:

In [23]:
df = pd.DataFrame({'Frequency':np.arange(10)})
df

Out[23]:
   Frequency
0          0
1          1
2          2
3          3
4          4
5          5
6          6
7          7
8          8
9          9

In [24]:
df['Cumulative Frequency'] = df['Frequency'].cumsum()
df

Out[24]:
   Frequency  Cumulative Frequency
0          0                     0
1          1                     1
2          2                     3
3          3                     6
4          4                    10
5          5                    15
6          6                    21
7          7                    28
8          8                    36
9          9                    45