python - 如何获取数据框列中每个值的前 n 个值和后 n 个值的平均值
问题描述
解释这一点的最简单方法应该是一个例子。
想象一下以下数据框:
a b
1 5
2 4
3 2
4 2
5 4
6 3
7 2
8 1
9 0
我希望能够获得 b 列中每个值的前 3 个值和后 3 个值的平均值。所以它应该看起来像这样
a b c
1 5
2 4
3 2
4 2 3.3
5 4 2.3
6 3 1.83
7 2
8 1
9 0
任何帮助表示赞赏
谢谢
解决方案
这是我使用 numpy 帮助的解决方案:
(df 是您的示例数据框)
length = df.shape[0] # Number of rows in the dataframe
windowSize = 3 # Since we are looking at top 3 and bottom 3 values
for i in range(windowSize, length-windowSize):
# Get the indexes (0-based) of the top 3 values
top3Idxs = np.arange(i - windowSize, i)
bottom3Idxs = np.arange(i + 1, i + 1 + windowSize)
# Get the values in column b at the proper indices
top3Vals = df.b.to_numpy()[top3Idxs]
bottom3Vals = df.b.to_numpy()[bottom3Idxs]
# Find the average of the top3Vals and bottom3Vals
avg = np.mean(np.concatenate((top3Vals, bottom3Vals)))
# Set the average at the proper index in column c
df.at[i, 'c'] = avg
推荐阅读
- graphql-tools - 如何使用 graphql-tools 处理单个对象中的嵌套对象
- mysql - 在查询mysql中获取最小记录
- wordpress - WordPress:如何获取某个类别的所有帖子中使用的所有标签?
- sql - 使用数字类型的数组进行 SQL 递归查询
- java - 如何启用搜索功能以从 Listview 中查找所有值?
- python-3.x - 如何更新嵌套字典中唯一键的值?
- android-mediaplayer - MediaPlayer 上的低音量 - Android Things
- machine-learning - 为什么我们在卷积神经网络中在卷积层之后使用激活函数?
- html - HTML 列表中断
- android - 没有活动或支持库的 PermissionChecker