熊猫:熊猫数据框下的某些查找功能

时间:2018-12-08 07:41:47

标签: python pandas

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

部分结果:

           High   Low  Open  Close       Volume  Adj Close  Gain  Loss  Avg Gain  Avg Loss        RSI
Date                                                                                                  
2018-07-03  3.87  3.76  3.83   3.84  684899302.0   3.629538   NaN   NaN       NaN       NaN        NaN
2018-07-04  3.91  3.84  3.86   3.86  460325574.0   3.648442  0.02  0.00       NaN       NaN        NaN
2018-07-05  3.70  3.62  3.68   3.68  292810499.0   3.680000  0.00 -0.18       NaN       NaN        NaN
2018-07-06  3.72  3.61  3.69   3.67  343653088.0   3.670000  0.00 -0.01       NaN       NaN        NaN
2018-07-09  3.75  3.68  3.70   3.69  424596186.0   3.690000  0.02  0.00       NaN       NaN        NaN
2018-07-10  3.74  3.70  3.71   3.71  327048051.0   3.710000  0.02  0.00       NaN       NaN        NaN
2018-07-11  3.65  3.61  3.63   3.64  371355401.0   3.640000  0.00 -0.07       NaN       NaN        NaN
2018-07-12  3.69  3.63  3.66   3.66  309888328.0   3.660000  0.02  0.00       NaN       NaN        NaN
2018-07-13  3.69  3.62  3.69   3.63  261928758.0   3.630000  0.00 -0.03       NaN       NaN        NaN
2018-07-16  3.63  3.57  3.61   3.62  306970074.0   3.620000  0.00 -0.01       NaN       NaN        NaN
2018-07-17  3.62  3.56  3.62   3.58  310294921.0   3.580000  0.00 -0.04       NaN       NaN        NaN
2018-07-18  3.61  3.55  3.58   3.58  334592695.0   3.580000  0.00  0.00       NaN       NaN        NaN
2018-07-19  3.61  3.56  3.61   3.56  211984563.0   3.560000  0.00 -0.02       NaN       NaN        NaN
2018-07-20  3.64  3.52  3.57   3.61  347506394.0   3.610000  0.05  0.00       NaN       NaN        NaN
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
2018-08-14  3.58  3.53  3.58   3.56  243870383.0   3.560000  0.00  0.00  0.008058 -0.016243  33.158019
2018-08-15  3.56  3.51  3.54   3.53  282427652.0   3.530000  0.00 -0.03  0.007418 -0.017336  29.966819
2018-08-16  3.57  3.49  3.50   3.52  275769630.0   3.520000  0.00 -0.01  0.006833 -0.016758  28.966085
2018-08-17  3.56  3.52  3.54   3.54  188357023.0   3.540000  0.02  0.00  0.007863 -0.015447  33.732515
2018-08-20  3.57  3.51  3.55   3.56  226473564.0   3.560000  0.02  0.00  0.008806 -0.014247  38.198441
2018-08-21  3.58  3.55  3.57   3.56  238534835.0   3.560000  0.00  0.00  0.008126 -0.013147  38.198441
2018-08-22  3.58  3.53  3.55   3.58  269029064.0   3.580000  0.02  0.00  0.009038 -0.012138  42.678636
2018-08-23  3.59  3.54  3.57   3.55  247570379.0   3.550000  0.00 -0.03  0.008348 -0.013502  38.204696
2018-08-24  3.55  3.52  3.53   3.54  279465912.0   3.540000  0.00 -0.01  0.007713 -0.013236  36.819162
2018-08-27  3.61  3.54  3.55   3.60  462469493.0   3.600000  0.06  0.00  0.011668 -0.012235  48.813844
2018-08-28  3.65  3.60  3.61   3.63  248882773.0   3.630000  0.03  0.00  0.013049 -0.011313  53.561107
2018-08-29  3.61  3.57  3.60   3.61  237332367.0   3.610000  0.00 -0.02  0.012070 -0.011965  50.217314
2018-08-30  3.63  3.55  3.62   3.57  185660963.0   3.570000  0.00 -0.04  0.011167 -0.014061  44.265314
2018-08-31  3.55  3.52  3.55   3.53  359900115.0   3.530000  0.00 -0.04  0.010335 -0.015993  39.254752
2018-09-03  3.54  3.49  3.52   3.52  305346975.0   3.520000  0.00 -0.01  0.009568 -0.015548  38.093832
2018-09-04  3.54  3.51  3.52   3.53  208551797.0   3.530000  0.01  0.00  0.009600 -0.014397  40.004620
2018-09-05  3.54  3.47  3.52   3.47  363913474.0   3.470000  0.00 -0.06  0.008890 -0.017765  33.352720
2018-09-06  3.48  3.41  3.46   3.44  397574154.0   3.440000  0.00 -0.03  0.008235 -0.018667  30.611900
2018-09-07  3.46  3.40  3.44   3.43  567329432.0   3.430000  0.00 -0.01  0.007630 -0.018030  29.734744
2018-09-10  3.42  3.36  3.42   3.39  320927280.0   3.390000  0.00 -0.04  0.007070 -0.019642  26.467750
2018-09-11  3.40  3.36  3.36   3.36  231749076.0   3.360000  0.00 -0.03  0.006552 -0.020400  24.310377
2018-09-12  3.36  3.31  3.35   3.32  377546352.0   3.320000  0.00 -0.04  0.006073 -0.021833  21.763243
2018-09-13  3.41  3.37  3.37   3.40  270315440.0   3.400000  0.08  0.00  0.011468 -0.020240  36.168232
2018-09-14  3.44  3.39  3.42   3.41  142324690.0   3.410000  0.01  0.00  0.011361 -0.018765  37.712062
2018-09-17  3.42  3.36  3.40   3.39  181671619.0   3.390000  0.00 -0.02  0.010535 -0.018855  35.844807
2018-09-18  3.41  3.36  3.37   3.40  287170628.0   3.400000  0.01  0.00  0.010496 -0.017485  37.510837
2018-09-19  3.44  3.40  3.42   3.42  230539211.0   3.420000  0.02  0.00  0.011185 -0.016216  40.820882
2018-09-20  3.47  3.42  3.45   3.43  175746914.0   3.430000  0.01  0.00  0.011100 -0.015040  42.461996
2018-09-21  3.54  3.46  3.47   3.52  393921372.0   3.520000  0.09  0.00  0.016813 -0.013951  54.650933
2018-09-24  3.50  3.44  3.46   3.46  239296460.0   3.460000  0.00 -0.06  0.015597 -0.017283  47.436285
2018-09-26  3.54  3.45  3.47   3.49  367428100.0   3.490000  0.03  0.00  0.016638 -0.016033  50.924753
2018-09-27  3.51  3.46  3.49   3.49  228112298.0   3.490000  0.00  0.00  0.015436 -0.014876  50.924753
2018-09-28  3.52  3.47  3.49   3.48  232063346.0   3.480000  0.00 -0.01  0.014322 -0.014524  49.650887
2018-10-02  3.47  3.38  3.45   3.40  286950375.0   3.400000  0.00 -0.08  0.013290 -0.019245  40.847746
2018-10-03  3.42  3.38  3.38   3.39  137098156.0   3.390000  0.00 -0.01  0.012332 -0.018579  39.895543
2018-10-04  3.38  3.33  3.38   3.33  286550800.0   3.330000  0.00 -0.06  0.011444 -0.021562  34.672910
2018-10-05  3.34  3.31  3.32   3.33  292804313.0   3.330000  0.00  0.00  0.010620 -0.020010  34.672910
2018-10-08  3.37  3.31  3.35   3.31  305245178.0   3.310000  0.00 -0.02  0.009856 -0.020009  33.002736
2018-10-09  3.35  3.30  3.30   3.33  173730676.0   3.330000  0.02  0.00  0.010586 -0.018571  36.306804
2018-10-10  3.38  3.33  3.36   3.34  215849513.0   3.340000  0.01  0.00  0.010544 -0.017236  37.954425
2018-10-11  3.28  3.22  3.24   3.27  595855447.0   3.270000  0.00 -0.07  0.009786 -0.021026  31.760877
2018-10-12  3.33  3.26  3.30   3.31  290519307.0   3.310000  0.04  0.00  0.011956 -0.019516  37.988157
2018-10-15  3.29  3.24  3.29   3.25  266829437.0   3.250000  0.00 -0.06  0.011098 -0.022422  33.107608
2018-10-16  3.28  3.23  3.26   3.25  272678220.0   3.250000  0.00  0.00  0.010301 -0.020813  33.107608
2018-10-18  3.27  3.22  3.27   3.23  241354726.0   3.230000  0.00 -0.02  0.009562 -0.020755  31.541056
2018-10-19  3.31  3.20  3.22   3.28  324848738.0   3.280000  0.05  0.00  0.012462 -0.019267  39.276709
2018-10-22  3.37  3.28  3.28   3.34  309029852.0   3.340000  0.06  0.00  0.015870 -0.017886  47.014077
2018-10-23  3.33  3.23  3.33   3.27  421680917.0   3.270000  0.00 -0.07  0.014732 -0.021620  40.526165
2018-10-24  3.32  3.25  3.28   3.27  286740826.0   3.270000  0.00  0.00  0.013677 -0.020071  40.526165
2018-10-25  3.27  3.22  3.22   3.27  228688814.0   3.270000  0.00  0.00  0.012697 -0.018634  40.526165
2018-10-26  3.28  3.23  3.25   3.26  195858287.0   3.260000  0.00 -0.01  0.011788 -0.018015  39.552287
2018-10-29  3.29  3.25  3.28   3.27  348004378.0   3.270000  0.01  0.00  0.011660 -0.016725  41.077173
2018-10-30  3.35  3.24  3.27   3.30  372317706.0   3.300000  0.03  0.00  0.012973 -0.015528  45.517552
2018-10-31  3.35  3.31  3.32   3.34  239423074.0   3.340000  0.04  0.00  0.014907 -0.014416  50.837351
2018-11-01  3.38  3.34  3.36   3.37  281115874.0   3.370000  0.03  0.00  0.015988 -0.013385  54.431140

问题: 如果“ RSI”低于25,我想为以下14行找到“关闭”的最大值。 例如, 我发现2018-09-12的RSI为21.763243,低于25。 然后,我想找到2018-09-13至2018-10-04之间的最大收盘价(14行)。 最终答案应该是3.52。 我可以在Python中执行此查找功能吗?

更新:

subdf = df.iloc[51:51+14]

它可以自动变为。 例如, 根据公式,我们知道第50和51行低于25。 但是在此阶段,我需要手动输入50和51。 python有什么办法可以帮到我,只需在下面的n中填写50和51:

subdf = df.iloc[n:n+14]

最后,结果将是3.52、3.52。

1 个答案:

答案 0 :(得分:0)

首先,您需要添加新索引以获取连续的行号:

df = df.reset_index()

以下是带有RSI<25的行:

df[df['RSI']<25]
         Date  High   Low  Open  Close     Volume  Adj Close  Gain  Loss  Avg Gain  Avg Loss        RSI
50 2018-09-11  3.40  3.36  3.36   3.36  231749076       3.36   0.0 -0.03  0.006552 -0.020400  24.310377
51 2018-09-12  3.36  3.31  3.35   3.32  377546352       3.32   0.0 -0.04  0.006073 -0.021833  21.763243

选择所需行的索引并创建一个新的数据框:

subdf = df.iloc[51:51+14]

然后找到最大值:

subdf['Close'].max()
3.52

或一行:

df.iloc[51:51+14]['Close'].max()