calculating average for segments in dataframe(计算数据帧中数据段的平均值)
本文介绍了计算数据帧中数据段的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的图片中,我有一个巨大的数据帧。对于每一次冲程,机器都会呈现穿透值,然后给零。我想计算一下每划一划的平均数。例如,[0.762,0.766]的平均值,以及[0.66,1.37,2.11,2.29]的平均值,依此类推,直到数据帧结束。请注意,笔划没有固定的长度。enter image description here
推荐答案
# Generate example data based on your image
df = pd.DataFrame({'penetration':
[0, 1, 0, 2, 3, 0, 0,
5, 6, 7, 0, 0, 0, 0]})
# Flag rows with nonzero penetration depth
df['segment'] = df['penetration'].ne(0)
# Flag rows which represent a change from non-segment
# to segment, or from segment to non-segment
df['group'] = df['segment'].ne(df['segment'].shift())
# Label each segment with a unique integer
df['group'].cumsum()
# Zero out the group number of rows which belong to non-segments
df['group'] = df['group'].where(df['segment'], 0)
# Get mean of penetration depths for each group
df['mean'] = df.groupby('group')['penetration'].transform('mean')
# Print result
df
penetration segment group mean
0 0 False 0 0.0
1 1 True 2 1.0
2 0 False 0 0.0
3 2 True 4 2.5
4 3 True 4 2.5
5 0 False 0 0.0
6 0 False 0 0.0
7 5 True 6 6.0
8 6 True 6 6.0
9 7 True 6 6.0
10 0 False 0 0.0
11 0 False 0 0.0
12 0 False 0 0.0
13 0 False 0 0.0
这篇关于计算数据帧中数据段的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:计算数据帧中数据段的平均值


猜你喜欢
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 沿轴计算直方图 2022-01-01