简体   繁体   English

Python Pandas-根据给定的窗口并从特定值开始计算特定列的总和

[英]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.

相关问题 Python Pandas-根据给定值删除行 - Python Pandas- remove rows based on given value 熊猫-根据另一列的行总数创建新列的正确方法(试图在副本上设置的值)? - Pandas- correct way to create a new column based on the sum of rows of another column (value trying to be set on a copy)? 根据某些条件在熊猫中计算新列 - Calculate new column in pandas based on certain criteria python&pandas-根据DataFrame列中的某些值计算出十二行 - python & pandas- Calculation bewteen rows based on certain values in columns from DataFrame 熊猫-如果大多数情况下具有特定值,如何删除行或列? - Pandas- How to drop a row or column if they have a certain value most of the times? Pandas Dataframe:对于给定的行,尝试基于在另一列中查找值来分配特定列中的值 - Pandas Dataframe: for a given row, trying to assign value in a certain column based on a lookup of a value in another column 只对熊猫数据框的给定列中的某些行求和 - Sum only certain rows in a given column of pandas dataframe Pandas-根据多列值查找平均值 - Pandas- Find average based on multiple column value 根据Pandas中给定列中的某个行值将数据集拆分为单独的excel文件? - Break up a data-set into separate excel files based on a certain row value in a given column in Pandas? Pandas groupby cumcount 从具有特定列值的行开始 - Pandas groupby cumcount starting on row with a certain column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM