[英]Python Pandas- Calculate sum of a certain column based on a given window and starting at a certain value
I am trying to create a new column in a Python Pandas data frame that contains summed values from another column within a specified range and starting at a certain row. 我正在尝试在Python Pandas数据框中创建一个新列,其中包含指定范围内并从某一行开始的另一列的求和值。 For example, in the data frame below, I want the sum of the Value column starting at row 3 (using index row numbers) for Trace 1. However, I also need to repeat the same criteria for Trace 2. Moreover, I need to start at the same Sample number for each Trace. 例如,在下面的数据框中,我希望跟踪1的值列的总和从第3行开始(使用索引行号)。但是,我还需要对跟踪2重复相同的条件。此外,我需要从每个跟踪的相同样本编号开始。 Any ideas? 有任何想法吗?
IIUC, IIUC,
groupby
+ rolling
+ sum
groupby
+ rolling
+ sum
df = pd.DataFrame({'trace': [1]*5 + [2]*5, 'sample': list(range(1,6))*2,
'value': [0.25, 0.63, 0.98, 0.48, 0.52,0.79, 0.63, 0.11, 0.29, 0.81]})
df.groupby('trace').value.rolling(4).sum()
1 0 NaN
1 NaN
2 NaN
3 2.34
4 2.61
2 5 NaN
6 NaN
7 NaN
8 1.82
9 1.84
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.