我有一个数据框 df
,其中列 A
中有浮点值。我想添加另一列 B
,使得:
B[0] = A[0]
对于i > 0...
B[i] = if(np.isnan(A[i])) then A[i] else Step3
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
B[i-1]
是np.nan
,你如何确定要在B[i]
中放置什么?因为那个 if 语句是不确定的。 - CJR