简体   繁体   English

如何计算 pandas dataframe 中每个数字的 3 个值的总和,包括第一个数字?

[英]How can I calculate the sum of 3 values from each number in a pandas dataframe including the first number?

I have a dataframe as below:我有一个 dataframe 如下:

 Datetime Data Fn 0 18747.385417 11275.0 0 1 18747.388889 8872.0 1 2 18747.392361 7050.0 0 3 18747.395833 8240.0 1 4 18747.399306 5158.0 1 5 18747.402778 3926.0 0 6 18747.406250 4043.0 0 7 18747.409722 2752.0 1 8 18747.420139 3502.0 1 9 18747.423611 4026.0 1

I want to calculate the sum of 3 values from each number in Fn and put the value in Sum .我想从Fn中的每个数字计算 3 个值的总和,并将该值放入Sum中。

My expected result is this:我的预期结果是这样的:

 Datetime Data Fn Sum 0 18747.385417 11275.0 0 0 1 18747.388889 8872.0 1 0 2 18747.392361 7050.0 0 1 3 18747.395833 8240.0 1 2 4 18747.399306 5158.0 1 2 5 18747.402778 3926.0 0 2 6 18747.406250 4043.0 0 1 7 18747.409722 2752.0 1 1 8 18747.420139 3502.0 1 2 9 18747.423611 4026.0 1 3
 df['Sum'] = df.Fn.rolling(3).sum().fillna(0)

Output: Output:

 Datetime Data Fn Sum 0 18747.385417 11275.0 0 0.0 1 18747.388889 8872.0 1 0.0 2 18747.392361 7050.0 0 1.0 3 18747.395833 8240.0 1 2.0 4 18747.399306 5158.0 1 2.0 5 18747.402778 3926.0 0 2.0 6 18747.406250 4043.0 0 1.0 7 18747.409722 2752.0 1 1.0 8 18747.420139 3502.0 1 2.0 9 18747.423611 4026.0 1 3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从pandas数据框中的每一列获取最大(x)值,同时保持每个索引的索引? - How can I get the max (x) number of values from each column in a pandas dataframe while keeping the index for each? 如何计算 pandas dataframe 中组内的列中连续值的数量? - How can I calculate number of consecutive values in a column within a group in a pandas dataframe? 如何从 Pandas 数据框中的每一行获取前三个最大值? - How can I get the first three max values from each row in a Pandas dataframe? Pandas计算每个范围之间的值的数量 - Pandas calculate number of values between each range 如何在 Pandas 中显示此 dataframe 的最大丢失次数? - How Can I display max number of loses from this dataframe in Pandas? 如何舍入 pandas dataframe 的特定列中每个数组中的每个数字? - How can I round each number in each array in specific columns of a pandas dataframe? 如何求和数字值的结果? - How can I sum the result of number values? 如何根据`pandas.DataFrame.index`计算两个`pandas.DataFrame`的总和? - How can I calculate the sum of two `pandas.DataFrame` based on `pandas.DataFrame.index`? 如何从数字中减去熊猫DataFrame的每一行? - How to subtract each row of a pandas DataFrame from a number? 计算第一个值出现在熊猫数据框中的天数 - Calculate number ofdays until first value appears in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM