Pandas:Pandas DataFrame中当前和之后单元的总和

时间:2018-12-08 14:21:27

标签: python 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

输出:

        Date    High    Low     Open    Close   Volume  Adj Close   Gain    Loss    Avg Gain    Avg Loss    RSI
0   2018-07-03  3.87    3.76    3.83    3.84    684899302.0 3.629538    NaN NaN NaN NaN NaN
1   2018-07-04  3.91    3.84    3.86    3.86    460325574.0 3.648442    0.02    0.00    NaN NaN NaN
2   2018-07-05  3.70    3.62    3.68    3.68    292810499.0 3.680000    0.00    -0.18   NaN NaN NaN
3   2018-07-06  3.72    3.61    3.69    3.67    343653088.0 3.670000    0.00    -0.01   NaN NaN NaN
4   2018-07-09  3.75    3.68    3.70    3.69    424596186.0 3.690000    0.02    0.00    NaN NaN NaN
5   2018-07-10  3.74    3.70    3.71    3.71    327048051.0 3.710000    0.02    0.00    NaN NaN NaN
6   2018-07-11  3.65    3.61    3.63    3.64    371355401.0 3.640000    0.00    -0.07   NaN NaN NaN
7   2018-07-12  3.69    3.63    3.66    3.66    309888328.0 3.660000    0.02    0.00    NaN NaN NaN
8   2018-07-13  3.69    3.62    3.69    3.63    261928758.0 3.630000    0.00    -0.03   NaN NaN NaN
9   2018-07-16  3.63    3.57    3.61    3.62    306970074.0 3.620000    0.00    -0.01   NaN NaN NaN
10  2018-07-17  3.62    3.56    3.62    3.58    310294921.0 3.580000    0.00    -0.04   NaN NaN NaN
11  2018-07-18  3.61    3.55    3.58    3.58    334592695.0 3.580000    0.00    0.00    NaN NaN NaN
12  2018-07-19  3.61    3.56    3.61    3.56    211984563.0 3.560000    0.00    -0.02   NaN NaN NaN
13  2018-07-20  3.64    3.52    3.57    3.61    347506394.0 3.610000    0.05    0.00    NaN NaN NaN
14  2018-07-23  3.65    3.57    3.59    3.62    313125328.0 3.620000    0.01    0.00    0.010594    -0.021042   33.487100
15  2018-07-24  3.71    3.60    3.60    3.68    367627204.0 3.680000    0.06    0.00    0.015854    -0.018802   45.745967
16  2018-07-25  3.73    3.68    3.72    3.69    270460990.0 3.690000    0.01    0.00    0.015252    -0.016868   47.483263
17  2018-07-26  3.73    3.66    3.72    3.69    234388072.0 3.690000    0.00    0.00    0.013731    -0.015186   47.483263
18  2018-07-27  3.70    3.66    3.68    3.69    190039532.0 3.690000    0.00    0.00    0.012399    -0.013713   47.483263
19  2018-07-30  3.72    3.67    3.68    3.70    163971848.0 3.700000    0.01    0.00    0.012172    -0.012417   49.502851
20  2018-07-31  3.70    3.66    3.67    3.68    168486023.0 3.680000    0.00    -0.02   0.011047    -0.013118   45.716244
21  2018-08-01  3.72    3.66    3.71    3.68    199801191.0 3.680000    0.00    0.00    0.010047    -0.011930   45.716244
22  2018-08-02  3.68    3.59    3.66    3.61    307920738.0 3.610000    0.00    -0.07   0.009155    -0.017088   34.884632
23  2018-08-03  3.62    3.57    3.59    3.61    184816985.0 3.610000    0.00    0.00    0.008356    -0.015596   34.884632
24  2018-08-06  3.66    3.60    3.62    3.61    189696153.0 3.610000    0.00    0.00    0.007637    -0.014256   34.884632
25  2018-08-07  3.66    3.61    3.63    3.65    216157642.0 3.650000    0.04    0.00    0.010379    -0.013048   44.302922
26  2018-08-08  3.66    3.61    3.65    3.63    215365540.0 3.630000    0.00    -0.02   0.009511    -0.013629   41.101805
27  2018-08-09  3.66    3.59    3.59    3.65    230275455.0 3.650000    0.02    0.00    0.010378    -0.012504   45.353992
28  2018-08-10  3.66    3.60    3.65    3.62    219157328.0 3.620000    0.00    -0.03   0.009530    -0.013933   40.617049
29  2018-08-13  3.59    3.54    3.58    3.56    270620120.0 3.560000    0.00    -0.06   0.008759    -0.017658   33.158019

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

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

# convert to date time
df['Date'] = pd.to_datetime(df['Date'])

# calculate max for 14 days
df['max_close_within_14_trade_days'] = df['Date'].map(df.groupby([pd.Grouper(key='Date', freq='14D')])['Close'].max())

# fill missing values by previous value
df['max_close_within_14_trade_days'].fillna(method='ffill', inplace=True)

    Date    High    Low    Open  Close    max_close_within_14_trade_days
0   0   2018-07-03  3.87    3.76    3.83    3.86
1   1   2018-07-04  3.91    3.84    3.86    3.86
2   2   2018-07-05  3.70    3.62    3.68    3.86
3   3   2018-07-06  3.72    3.61    3.69    3.86
4   4   2018-07-09  3.75    3.68    3.70    3.86

其他解决方案:

df['max_close_within_14_trade_days'] = [df.loc[x+1:x+14,'Close'].max() for x in range(0, df.shape[0])]