Python 如何处理熊猫中仅包含NaN值的单元格?
我正在建立一个股票价格预测数据集,同时为Ichimoku Cloud Indicator应用以下代码:Python 如何处理熊猫中仅包含NaN值的单元格?,python,pandas,time-series,Python,Pandas,Time Series,我正在建立一个股票价格预测数据集,同时为Ichimoku Cloud Indicator应用以下代码: from datetime import timedelta high_9 = df['High'].rolling(window= 9).max() low_9 = df['Low'].rolling(window= 9).min() df['tenkan_sen'] = (high_9 + low_9) /2 high_26 = df['High'].rolling(window= 26
from datetime import timedelta
high_9 = df['High'].rolling(window= 9).max()
low_9 = df['Low'].rolling(window= 9).min()
df['tenkan_sen'] = (high_9 + low_9) /2
high_26 = df['High'].rolling(window= 26).max()
low_26 = df['Low'].rolling(window= 26).min()
df['kijun_sen'] = (high_26 + low_26) /2
# this is to extend the 'df' in future for 26 days
# the 'df' here is numerical indexed df
# the problem is here
last_index = df.iloc[-1:].index[0]
last_date = df['Date'].iloc[-1].date()
for i in range(26):
df.loc[last_index+1 +i, 'Date'] = last_date + timedelta(days=i)
df['senkou_span_a'] = ((df['tenkan_sen'] + df['kijun_sen']) / 2).shift(26)
high_52 = df['High'].rolling(window= 52).max()
low_52 = df['Low'].rolling(window= 52).min()
df['senkou_span_b'] = ((high_52 + low_52) /2).shift(26)
# most charting softwares dont plot this line
df['chikou_span'] = df['Close'].shift(-26)
上面的代码工作得很好,但问题是当扩展到“senoku span a”和“b”列中接下来的26个时间步(行)时,它会将其他rest列行的值转换为NaN
因此,我需要帮助在我的数据集中生成“Senoku span a”和“Senoku span b”预测行,而无需将其他行变为NaN
电流输出为:
Date Open High Low Close Senoku span a Senoku span b
2019-03-16 50 51 52 53 56.0 55.82
2019-03-17 NaN NaN NaN NaN 55.0 56.42
2019-03-18 NaN NaN NaN NaN 54.0 57.72
2019-03-19 NaN NaN NaN NaN 53.0 58.12
2019-03-20 NaN NaN NaN NaN 52.0 59.52
Date Open High Low Close Senoku span a Senoku span b
2019-03-16 50 51 52 53 56.0 55.82
2019-03-17 55.0 56.42
2019-03-18 54.0 57.72
2019-03-19 53.0 58.12
2019-03-20 52.0 59.52
预期产出为:
Date Open High Low Close Senoku span a Senoku span b
2019-03-16 50 51 52 53 56.0 55.82
2019-03-17 NaN NaN NaN NaN 55.0 56.42
2019-03-18 NaN NaN NaN NaN 54.0 57.72
2019-03-19 NaN NaN NaN NaN 53.0 58.12
2019-03-20 NaN NaN NaN NaN 52.0 59.52
Date Open High Low Close Senoku span a Senoku span b
2019-03-16 50 51 52 53 56.0 55.82
2019-03-17 55.0 56.42
2019-03-18 54.0 57.72
2019-03-19 53.0 58.12
2019-03-20 52.0 59.52
您想要的预期输出是什么?我想要我的数据集中的预测列值,而不将任何其他值转换为NaN.df=df.fillna(“”)是的,您可以在最终数据帧上使用df=df.fillna(“”)。或者您也可以将其应用于特定列,如df['open']=df['open'].fillna(“”)但是这段代码给应用其他代码带来了问题,当应用其他代码时,它会显示错误为:不支持的操作数类型-:“str”和“float”的预期输出是什么您想要我的数据集中的预测列值,而不将任何其他值转换为NaN.df=df.fillna(“”)是的您可以使用df=df.fillna(“”)在最后的数据帧上。或者也可以将其应用于特定列,如df['open']=df['open'].fillna(“”),但此代码会给其他代码的应用带来问题,而在应用其他代码时,它会显示错误为:不支持的操作数类型为-:'str'和'float'