cra*_*rog 5 python loops numpy vectorization
我想以矢量化方式编写以下代码,因为当前代码非常慢(并且想学习Python最佳实践).基本上,代码说如果今天的价值在昨天价值的10%之内,那么今天的价值(在新栏目中)与昨天的价值相同.否则,今天的价值不变:
def test(df):
df['OldCol']=(100,115,101,100,99,70,72,75,78,80,110)
df['NewCol']=df['OldCol']
for i in range(1,len(df)-1):
if df['OldCol'][i]/df['OldCol'][i-1]>0.9 and df['OldCol'][i]/df['OldCol'][i-1]<1.1:
df['NewCol'][i]=df['NewCol'][i-1]
else:
df['NewCol'][i]=df['OldCol'][i]
return df['NewCol']
Run Code Online (Sandbox Code Playgroud)
输出应如下:
OldCol NewCol
0 100 100
1 115 115
2 101 101
3 100 101
4 99 101
5 70 70
6 72 70
7 75 70
8 78 70
9 80 70
10 110 110
Run Code Online (Sandbox Code Playgroud)
你能帮忙吗?
我想使用这样的东西,但我无法解决我的问题:
def test(df):
df['NewCol']=df['OldCol']
cond=np.where((df['OldCol'].shift(1)/df['OldCol']>0.9) & (df['OldCol'].shift(1)/df['OldCol']<1.1))
df['NewCol'][cond[0]]=df['NewCol'][cond[0]-1]
return df
Run Code Online (Sandbox Code Playgroud)
您应该对原始数据框进行布尔掩码:
df[(0.9 <= df['NewCol']/df['OldCol']) & (df['NewCol']/df['OldCol'] <= 1.1)]NewCol将为您提供10% 以内的所有行OldCol
因此要设置NewCol这些行中的字段:
within_10 = df[(0.9 <= df['NewCol']/df['OldCol']) & (df['NewCol']/df['OldCol'] <= 1.1)]
within_10['NewCol'] = within_10['OldCol']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
155 次 |
| 最近记录: |