Pandas:对 Pandas DataFrame 中的当前和后续单元格求和

Python代码


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from pandas_datareader import data as wb


stock='3988.HK'


df = wb.DataReader(stock,data_source='yahoo',start='2018-07-01')


rsi_period = 14


chg = df['Close'].diff(1)


gain = chg.mask(chg<0,0)

df['Gain'] = gain


loss = chg.mask(chg>0,0)

df['Loss'] = loss


avg_gain = gain.ewm(com = rsi_period-1,min_periods=rsi_period).mean()

avg_loss = loss.ewm(com = rsi_period-1,min_periods=rsi_period).mean()


df['Avg Gain'] = avg_gain

df['Avg Loss'] = avg_loss


rs = abs(avg_gain/avg_loss)


rsi = 100-(100/(1+rs))


df['RSI'] = rsi


df.reset_index(inplace=True)


df

在这种情况下,我想创建一个新列“14 个交易日内的最大收盘价”。“14 个交易日内的最大收盘价”=接下来 14 天内的最大“收盘价”。例如,在第 0 行,数据范围应该是从第 1 行到第 15 行,“14 个交易日内的最大收盘价”= 3.86


慕尼黑5688855
浏览 241回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python