我有以下資料框:
d = {'T': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'Val1': [10, np.NaN, 14, np.NaN, np.NaN, np.NaN, 20, np.NaN, np.NaN, 30]}
df = pd.DataFrame(data=d)
T Val1
1 10.0
2 NaN
3 14.0
4 NaN
5 NaN
6 NaN
7 20.0
8 NaN
9 NaN
10 30.0
我想根據某些條件用不同的值填充 NaN:
- 如果值
V是 NaN 并且如果V 1和V-1不是 NaN 那么V=np.mean([V 1, V-1]) - 如果值
V和V 1是 NaN 并且如果V-1和V 2不是 NaN 那么我想按照這個公式填充它們:V=np.cbrt([(V-1)*(V-1)*(V 2)])ANDV 1=np.cbrt([(V-1)*(V 2)*(V 2)]) - 應洗掉其他 NaN
所以想要的資料表應該是這樣的:
T Val1
1 10.0
2 12.0
3 14.0
7 20.0
8 22.89
9 26.20
10 30.0
我能夠V=np.mean([V 1, V-1])通過以下命令做到這一點:
df1 = pd.concat([df.ffill(), df.bfill()]).groupby(level=0).mean()
T Val1
1 10.0
2 12.0
3 14.0
4 17.0
5 17.0
6 17.0
7 20.0
8 25.0
9 25.0
10 30.0
但我不知道如何結合不同的條件。我嘗試使用np.select()但我找不到恢復以下和先前值并將它們添加到條件的方法。
非常感謝
uj5u.com熱心網友回復:
您可以使用:
def condition_2(a, b): #a = V-1, b = V 2
return np.cbrt((a) * (a) * (b))
def condition_3(a,b): # a = V-2, b=V 1
return np.cbrt((a) * (b) * (b))
d = {'T': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'Val1': [10, np.NaN, 14, np.NaN, np.NaN, np.NaN, 20, np.NaN, np.NaN, 30]}
df = pd.DataFrame(data=d)
cond_1 = df['Val1'].isnull() & df['Val1'].shift(1).notna() & df['Val1'].shift(-1).notna()
cond_2 = df['Val1'].isnull() & df['Val1'].shift(1).notna() & df['Val1'].shift(-1).isnull() & df['Val1'].shift(-2).notna()
cond_3 = df['Val1'].isnull() & df['Val1'].shift(-1).notna() & df['Val1'].shift(1).isnull() & df['Val1'].shift(2).notna()
df['Val1'] = np.where(cond_1, (df['Val1'].shift(1) df['Val1'].shift(-1))/2, df['Val1'])
df['Val1'] = np.where(cond_2, condition_2(df['Val1'].shift(1), df['Val1'].shift(-2)), df['Val1'])
df['Val1'] = np.where(cond_3, condition_3(df['Val1'].shift(2), df['Val1'].shift(-1)), df['Val1'])
df.dropna(subset=['Val1'], inplace=True)
OUTPUT
T Val1
0 1 10.000000
1 2 12.000000
2 3 14.000000
6 7 20.000000
7 8 22.894285
8 9 26.207414
9 10 30.000000
uj5u.com熱心網友回復:
這是使用np.split和自定義函式的一種解決方案。基本上拆分非 NaN 值并迭代每個拆分以評估是否洗掉 NaN 或更改 NaN:
def nan2notna(arr1, arr2):
mask = pd.isna(arr1)
if len(arr1[mask]) > 2:
return arr1[~mask]
else:
if len(arr1[mask]) == 2:
arr1[mask] = [np.cbrt([(arr1.iloc[0])*(arr1.iloc[0])*(arr2.iloc[0])]), np.cbrt([(arr1.iloc[0])*(arr2.iloc[0])*(arr2.iloc[0])])]
elif len(arr1[mask]) == 1:
arr1[mask] = np.mean([arr1.iloc[0], arr2.iloc[0]])
else:
pass
return arr1
splits = np.split(df['Val1'], np.where(pd.notna(df['Val1']))[0])[1:]
out = (df.merge(pd.concat([nan2notna(arr1, arr2) for (arr1, arr2) in zip(splits, splits[1:] [None])]).to_frame(),
left_index=True, right_index=True)
.drop(columns='Val1_x')
.rename(columns={'Val1_y':'Val1'})
.round(2))
輸出:
T Val1
0 1 10.00
1 2 12.00
2 3 14.00
6 7 20.00
7 8 22.89
8 9 26.21
9 10 30.00
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/390549.html
