[英]Sum values in column based on values in other column and append to df - python
I have a df that looks like this: df here我有一个看起来像这样的 df: df here
The column Count gives values that are either 0 or stretches of higher numbers separated by zeros eg 0,0,0,0,4,4,4,4,4,4,0,0,0,0,0,0,6,6,6,6,6,6,6,0,0,0,0.列 Count 给出的值要么是 0,要么是用零分隔的更高数字的延伸,例如 0,0,0,0,4,4,4,4,4,4,0,0,0,0,0,0, 6,6,6,6,6,6,6,0,0,0,0。
I would like to sum the values of the column "angle_1frame_abs" for each stretch and add it as new column to the df eg我想对每个拉伸的列“angle_1frame_abs”的值求和并将其作为新列添加到 df 例如
Count: 0,0,0,0,4,4,4,4,4,4,...计数:0,0,0,0,4,4,4,4,4,4,...
angle_1frame_abs: 2,1,3,4,2,2,2,3,4,3,... angle_1frame_abs: 2,1,3,4,2,2,2,3,4,3,...
new column: 10,10,10,10,16,16,16,16,16,16,...新列:10,10,10,10,16,16,16,16,16,16,...
Thank you all!谢谢你们!
Seems what you need is to divide your df into segments with the same consecutive value of Count
, and sum over the angle_1frame_abs
within each segment, and copy the sum of each segment to each row of the segment.似乎您需要的是将 df 分成具有相同连续值Count
的段,并对每个段内的angle_1frame_abs
求和,并将每个段的总和复制到段的每一行。
In your case we cannot use Count
as the key for groupby
, so we need a new one:在你的情况下,我们不能使用Count
作为groupby
的键,所以我们需要一个新的:
new_key = (df['Count'].diff().abs() > 0).cumsum()
Using the new key, we can do the sum:使用新密钥,我们可以求和:
the_sum = df.groupby( new_key )['angle_1frame_abs'].sum()
And copy the summation results back to the table by the new key并通过新key将求和结果复制回表中
df['NewColumn'] = new_key.map(the_sum)
Note that we use diff()
to find out the boundary of segments, and used abs() > 0
to convert boundary values to True/False
or 1/0
, then use cumsum
to get the new key by leveraging the fact that the cumsum doesn't change outside the boundary (ie within the segment).请注意,我们使用diff()
找出段的边界,并使用abs() > 0
将边界值转换为True/False
或1/0
,然后使用cumsum
通过利用 cumsum 的事实来获取新密钥不会在边界外(即在段内)发生变化。
I tested my code using the following fake data我使用以下虚假数据测试了我的代码
df = pd.DataFrame({
'Count': [2,0,0,0,4,4,4,4,4,4,0,0,0,0,0,0,6,6,6,6,6,6,6,0,0,0,1],
'angle_1frame_abs': [np.nan,0,0,0,4,4,4,4,4,4,1,1,1,1,1,1,6,6,6,6,6,6,6,2,2,2,2],
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.