简体   繁体   English

如何用其他列的计算值替换熊猫数据框中的 NaN

[英]How to replace NaN in pandas dataframe with calculated value from other columns

I have below dataframe where I added last row as latest data.我在下面的数据框中添加了最后一行作为最新数据。

df.tail()

            Open    High    Low     Close   %K      %D
Date                        
2022-06-22  23.71   25.45   23.55   24.29   21.74   18.01
2022-06-23  24.94   25.57   24.17   25.33   31.30   25.15
2022-06-24  26.11   28.04   26.05   27.98   51.99   35.01
2022-06-27  28.35   28.50   27.00   27.32   47.54   43.61
2022-06-28  27.46   28.21   24.76   24.83   NaN     NaN

I want to fill NaN values with calculated values with data from other columns.我想用来自其他列的数据的计算值填充 NaN 值。

I can do it with below code,我可以用下面的代码做到这一点,

df['14-high'] = df['High'].rolling(14).max()
df['14-low'] = df['Low'].rolling(14).min()
df['%K'] = (df['Close'] - df['Low'].rolling(14).min())*100/(df['14-high'] - df['14-low'])
df['%D'] = df['%K'].rolling(3).mean()

df.drop(columns=['14-high', '14-low'], inplace=True)

Is there a better way to do it without re calculating columns %K and %D?有没有更好的方法可以在不重新计算 %K 和 %D 列的情况下做到这一点? ie calculate only NaN cells instead of the whole column.即只计算 NaN 单元格而不是整个列。

One way to do this would be to use the pandas fillna() method .一种方法是使用pandas fillna() 方法
You will still need the first calculations:您仍然需要第一次计算:

df['14-high'] = df['High'].rolling(14).max()
df['14-low'] = df['Low'].rolling(14).min()

But then you can only update the NaN-values in the %K and %D columns:但是您只能更新 %K 和 %D 列中的 NaN 值:

df['%K'].fillna((df['Close'] - df['Low'].rolling(14).min())*100/(df['14-high'] - df['14-low']), inplace=True)
df['%D'].fillna(df['%K'].rolling(3).mean(), inplace=True)

Hope I could help!希望我能帮上忙!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM