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
这篇关于计算数据帧中数据段的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:计算数据帧中数据段的平均值


基础教程推荐
猜你喜欢
- 症状类型错误:无法确定关系的真值 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01