gpt4 book ai didi

python - 有条件地填写 Pandas 数据框

转载 作者:太空狗 更新时间:2023-10-30 00:58:19 24 4
gpt4 key购买 nike

我有一个数据框 df列中有浮点值 A .我想添加另一列 B这样:

  1. B[0] = A[0]

    i > 0 ...

  2. B[i] = if(np.isnan(A[i])) then A[i] else Step3
  3. B[i] = if(abs((B[i-1] - A[i]) / B[i-1]) < 0.3) then B[i-1] else A[i]

示例数据框 df可以按照下面给出的方式生成

import numpy as np
import pandas as pd
df = pd.DataFrame(1000*(2+np.random.randn(500, 1)), columns=list('A'))
df.loc[1, 'A'] = np.nan
df.loc[15, 'A'] = np.nan
df.loc[240, 'A'] = np.nan
df.loc[241, 'A'] = np.nan

最佳答案

这可以通过 Numba 相当有效地完成。 .如果您不能使用 Numba,只需省略 @njit,您的逻辑将作为 Python 级循环运行。

import numpy as np
import pandas as pd
from numba import njit

np.random.seed(0)
df = pd.DataFrame(1000*(2+np.random.randn(500, 1)), columns=['A'])
df.loc[1, 'A'] = np.nan
df.loc[15, 'A'] = np.nan
df.loc[240, 'A'] = np.nan

@njit
def recurse_nb(x):
out = x.copy()
for i in range(1, x.shape[0]):
if not np.isnan(x[i]) and (abs(1 - x[i] / out[i-1]) < 0.3):
out[i] = out[i-1]
return out

df['B'] = recurse_nb(df['A'].values)

print(df.head(10))

A B
0 3764.052346 3764.052346
1 NaN NaN
2 2978.737984 2978.737984
3 4240.893199 4240.893199
4 3867.557990 4240.893199
5 1022.722120 1022.722120
6 2950.088418 2950.088418
7 1848.642792 1848.642792
8 1896.781148 1848.642792
9 2410.598502 2410.598502

关于python - 有条件地填写 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54043086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com