[英]Python Pandas function which computes over previous row and current row
正在進行中,使用cumsum
和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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.