python - Python Pandas 函数计算前一行和当前行
问题描述
给一个我想转换为另一个的 DataFrame,其中转换函数对前一行和当前行进行操作。在给定的示例中,
RowID =1 is copied as is,
但从 RowID 2 开始,
Current Cell = Max(CurrentCell, Previous Cell * 0.9999 )
H4 <= MAX(B4, H3*0.9999)
where,
B4 = original value,
H3 = the previous row **'After Transformation'**
解决方案
这是在路上,用cumsum
and创建你自己的函数cumcount
,然后update
是列
def yourfun(s):
s.update(s.mask(s==0).ffill()-s.groupby(s.mask(s==0).eq(1).cumsum()).cumcount()*0.0001)
return s
df.apply(yourfun)
Out[291]:
1 2 3 4
0 0.0000 0.0000 1.0000 0.0000
1 0.0000 1.0000 0.9999 1.0000
2 0.0000 0.9999 0.9998 0.9999
3 1.0000 0.9998 0.9997 1.0000
4 0.9999 1.0000 1.0000 0.9999
5 0.9998 0.9999 0.9999 0.9998
6 1.0000 0.9998 0.9998 0.9997
推荐阅读
- php - 通过 $_GET 方法从 ajax 中的 php 文件的标题中获取值
- c# - 可编辑组合框无法正常工作
- smtpclient - NodeMailer - SMTP 传输连接池
- python - 在python中有效地使用来自csv的数据生成对象
- python - 如何使用 Pandas 获得增加值的平均值?
- ruby-on-rails - 使用带有 Rails4 的 Searchkick 导入记录时,如何强制预先加载关联?
- html - bootstrap 4中的重叠列
- reactjs - 如何从 React Native 中的包中动态获取数据
- python - 在另一个 Python 脚本中使用 POST 方法创建 Django 模型实例
- javascript - 如何使用 html5 canvas + js 进行粒子缩放过渡?