Python-计算不同组中的范围(最高-最低)

Python-计算不同组中的范围(最高-最低),第1张

Python-计算不同组中的范围(最高-最低)

那是你要的吗?

In [67]: dfOut[67]:       Open        High         Low       Close    Volume   Adj Close       WeekDate2015-09-14  116.580002  116.889999  114.860001  115.309998  58363400  112.896168 2015-09-182015-09-15  115.930000  116.529999  114.419998  116.279999  43341200  113.845864 2015-09-182015-09-16  116.250000  116.540001  115.440002  116.410004  37173500  113.973148 2015-09-182015-09-17  115.660004  116.489998  113.720001  113.919998  64112600  111.535266 2015-09-182015-09-18  112.209999  114.300003  111.870003  113.449997  74285300  111.075104 2015-09-182015-09-21  113.669998  115.370003  113.660004  115.209999  50222000  112.798263 2015-09-252015-09-22  113.379997  114.180000  112.519997  113.400002  50346200  111.026155 2015-09-252015-09-23  113.629997  114.720001  113.300003  114.320000  35756700  111.926895 2015-09-252015-09-24  113.250000  115.500000  112.370003  115.000000  50219500  112.592660 2015-09-252015-09-25  116.440002  116.690002  114.019997  114.709999  56151900  112.308730 2015-09-25In [68]: df.groupby('Week').apply(lambda x: x.High.max() - x.Low.min())Out[68]:Week2015-09-18    5.0199962015-09-25    4.319999dtype: float64

设置DF:

In [75]: from pandas_datareader import data as webIn [76]: df = web.DataReader('aapl', 'yahoo', '2015-09-14', '2015-09-25')In [77]: df.ix[:5, 'Week'] = df.index[df.index.weekday == 4][0]In [78]: df.ix[5:, 'Week'] = df.index[df.index.weekday == 4][-1]In [79]: dfOut[79]:       Open        High         Low       Close    Volume   Adj Close       WeekDate2015-09-14  116.580002  116.889999  114.860001  115.309998  58363400  112.896168 2015-09-182015-09-15  115.930000  116.529999  114.419998  116.279999  43341200  113.845864 2015-09-182015-09-16  116.250000  116.540001  115.440002  116.410004  37173500  113.973148 2015-09-182015-09-17  115.660004  116.489998  113.720001  113.919998  64112600  111.535266 2015-09-182015-09-18  112.209999  114.300003  111.870003  113.449997  74285300  111.075104 2015-09-182015-09-21  113.669998  115.370003  113.660004  115.209999  50222000  112.798263 2015-09-252015-09-22  113.379997  114.180000  112.519997  113.400002  50346200  111.026155 2015-09-252015-09-23  113.629997  114.720001  113.300003  114.320000  35756700  111.926895 2015-09-252015-09-24  113.250000  115.500000  112.370003  115.000000  50219500  112.592660 2015-09-252015-09-25  116.440002  116.690002  114.019997  114.709999  56151900  112.308730 2015-09-25


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5648309.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存