Python Pumps Cumprod问题

时间:2018-09-16 14:51:00

标签: python pandas dataframe

我仍在努力使用以下代码:

xa= [0, 0, 0, 0, 65, 67, 69, 75, 0, 0, 0]
xb = [.3, .3, .3,.3, .3, .3, .3, .3, .3, .3, .3]
ideal = [0, 0, 0, 0, 65, 67, 69, 75, 67.5, 60.75, 54.675]

df = pd.DataFrame({'a':xa, 'b':xb, 'i':ideal})

mask=(df['a']<51) & (df['b']>0)
df['c'] = df['a'].where(mask,0.9).groupby(~mask.cumsum()).cumprod()

print(df)

我希望“ c”列变得像“理想”。这只是我的100K +行完整数据集的示例。

“掩码”的计算如下: 当'a'{i} <51 AND'b'{i}> 0时?然后为真,否则为假

'c'列的计算方式如下: 当'mask'{i} = FALSE时,则'c'{i} ='a'{i}否则为'c'{i} = 0.9 *'c'{i-1}

所以我希望(一天)“ c”变得像“理想”一样。...

2 个答案:

答案 0 :(得分:1)

我相信这可以解决您的问题:

# First calculate the column as if there is no decay
mask=(df['a']<51) & (df['b']>0)
df['c'] = df['a'].where(~mask)
df['c'].fillna(method='ffill', inplace=True)
df['c'].fillna(0, inplace=True)

# Check how many rows since the mask has changed from True to False or v.v.
df['ones'] = 1
df['power'] = df['ones'].groupby((mask != mask.shift()).cumsum()).transform('cumsum')
# For the values in the mask, apply the decay
df['c'] = np.where(mask, 0.9 ** df['power']*df['c'], df['c'])
print(df)

输出:

     a    b       i       c  power ones
0    0  0.3   0.000   0.000       1     1
1    0  0.3   0.000   0.000       2     1
2    0  0.3   0.000   0.000       3     1
3    0  0.3   0.000   0.000       4     1
4   65  0.3  65.000  65.000       1     1
5   67  0.3  67.000  67.000       2     1
6   69  0.3  69.000  69.000       3     1
7   75  0.3  75.000  75.000       4     1
8    0  0.3  67.500  67.500       1     1
9    0  0.3  60.750  60.750       2     1
10   0  0.3  54.675  54.675       3     1

主要技巧是定义一列,该列定义将0.9乘以多少倍,而另一列则向前填充以检查如果没有衰减,则应为多少。希望这会有所帮助!

答案 1 :(得分:0)

如果我理解正确

mask=(df['a']<51) & (df['b']>0)
df['c'] = np.where(mask, df['i'].shift() * 0.9, df['i'])
df.fillna(0, inlace = True)

    a   b   i       c
0   0   0.3 0.000   0.000
1   0   0.3 0.000   0.000
2   0   0.3 0.000   0.000
3   0   0.3 0.000   0.000
4   65  0.3 65.000  65.000
5   67  0.3 67.000  67.000
6   69  0.3 69.000  69.000
7   75  0.3 75.000  75.000
8   0   0.3 67.500  67.500
9   0   0.3 60.750  60.750
10  0   0.3 54.675  54.675