简体   繁体   English

Python Pandas Dataframe永久性cumprod

[英]Python Pandas Dataframe perpetual cumprod

I have the following sample of the Dataframe (population rows 100k+): 我有以下数据框示例(填充行100k +):

In: 在:

    official        delta
    0               0.000201567           
    0               0.000194400            
    0               0.000151906            
    62.94957331     0.000144387            
    64.06471633     0.000125152            
    64.51335098     0.000133459            
    64.4101024      0.000120795            
    0               0.000146456

but receive the following output: 但收到以下输出:

official        delta               result
0               0.000201567         0
0               0.0001944           0 
0               0.000151906         0
62.94957331     0.000144387         0
64.06471633     0.000125152         0
64.51335098     0.000133459         0
64.4101024      0.000120795         0
0               0.000146456         0

Desired solution: 所需解决方案:

official     delta          result
0            0.000201567    0
0            0.0001944      0
0            0.000151906    0
62.94957331  0.000144387    62.94957331
64.06471633  0.000125152    64.06471633
64.51335098  0.000133459    64.51335098
64.4101024   0.000120795    64.4101024
0            0.000146456    63.76600137

I tried the following code although it seems that it does not work correctly. 我尝试了以下代码,尽管似乎无法正常工作。 I do not understand why it gives a fault result. 我不明白为什么会给出错误的结果。 When I execute it in a demo dataframe, everything is fine. 当我在演示数据框中执行它时,一切都很好。

The code should pick up the 'official' element when mask is True otherwise multiply its previous element with 0.99 . 当mask为True时,代码应选择“官方”元素,否则将其先前的元素乘以0.99。 The issue here is that when the mask is True, the code does not pick the 'official' element. 这里的问题是,当掩码为True时,代码不会选择“官方”元素。

mask = (df['official']<51) & (df['delta']>0)

df['result'] = df['official'].where(mask,0.99).groupby(~mask.cumsum()).cumprod()
mask = ~((df['official'] < 51) & (df['delta'] > 0))    
df['result1'] = df['official'].where(mask, 0.99*df['official'].shift(1)).fillna(0.0)

Add a unary operator to mask with ~ . 添加一元运算符~ mask If the mask element is True, that element, otherwise, shift 'official' down by one row and multiply by 0.99. 如果mask元素为True,则该元素为True,否则将“官方”下移1行并乘以0.99。 Fill the first element which will be NaN caused by the shift. 填充第一个元素,该元素将是由移位引起的NaN

Result: 结果:

    official     delta     result    result1
0   0.000000  0.000202   0.000000   0.000000
1   0.000000  0.000194   0.000000   0.000000
2   0.000000  0.000152   0.000000   0.000000
3  62.949573  0.000144  62.949573  62.949573
4  64.064716  0.000125  64.064716  64.064716
5  64.513351  0.000133  64.513351  64.513351
6  64.410102  0.000121  64.410102  64.410102
7   0.000000  0.000146  63.766001  63.766001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM