python - 在python中每五行相乘
问题描述
我有两个以下形式的熊猫数据框:
df1 df2
column factor
0 2 0 0.0
1 4 1 0.25
2 12 2 0.50
3 5 3 0.99
4 4 4 1.00
5 15
6 32
工作是将df1中的每5行与df2相加,并将新结果放入df3(我的实际数据在df1中有大约500行)。结果应该是这样的:
df3
results **description (no need to add this column)**
0 15.95 df1.iloc[:4,0].dot(df2)
1 24.46 df1.iloc[1:5,0].dot(df2)
3 50.10 df1.iloc[2:6,0].dot(df2
解决方案
尝试:
import numpy as np
mx=df2.to_numpy()
df1.rolling(5).apply(lambda x: np.dot(x, mx), raw=True).iloc[4:]
输出:
column
4 15.95
5 24.46
6 50.10
推荐阅读
- javascript - 电子邮件地址格式错误
- c# - SQL Server Hierarchyid 和实体框架 HierarchyId 的区别
- java - 如何从第二类的@beforemethod 调用第一类的@Test(POM 数据驱动方法)
- javascript - JavaScript 数组重构
- python - excel保存过程中的字符不正确
- macos - 无法在 macOS 应用程序的 FFMPEG 进程中通过 NSOpenPanel 访问用户选择的文件
- parallel-processing - 循环依赖分析:这个循环可以并行化吗?
- php - 显示下一个字段悬停切换按钮
- python - 如何在python中执行两个大熊猫文件的交叉连接
- html - 为数组对象管理我的 *ngFor 时出现问题