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

标签 python pandas dataframe group-by

我已经对我的数据进行了分组。现在,我要做的是每周从“高”列中选择最高值,并从“低”列中选择最低值,然后使用最高值减去最低值得到范围。但是代码总是错误的。有人对我有想法吗?

这是我的 DataFrame 的一部分:

enter image description here

还有我的错误代码:

grouped=df.groupby('week')
def Range(x,y):
    return x.max()-y.min()
grouped.agg(Range(grouped['high'],grouped['low']))

最佳答案

这是你想要的吗?

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

In [68]: df.groupby('Week').apply(lambda x: x.High.max() - x.Low.min())
Out[68]:
Week
2015-09-18    5.019996
2015-09-25    4.319999
dtype: float64

设置 DF:

In [75]: from pandas_datareader import data as web

In [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]: df
Out[79]:
                  Open        High         Low       Close    Volume   Adj Close       Week
Date
2015-09-14  116.580002  116.889999  114.860001  115.309998  58363400  112.896168 2015-09-18
2015-09-15  115.930000  116.529999  114.419998  116.279999  43341200  113.845864 2015-09-18
2015-09-16  116.250000  116.540001  115.440002  116.410004  37173500  113.973148 2015-09-18
2015-09-17  115.660004  116.489998  113.720001  113.919998  64112600  111.535266 2015-09-18
2015-09-18  112.209999  114.300003  111.870003  113.449997  74285300  111.075104 2015-09-18
2015-09-21  113.669998  115.370003  113.660004  115.209999  50222000  112.798263 2015-09-25
2015-09-22  113.379997  114.180000  112.519997  113.400002  50346200  111.026155 2015-09-25
2015-09-23  113.629997  114.720001  113.300003  114.320000  35756700  111.926895 2015-09-25
2015-09-24  113.250000  115.500000  112.370003  115.000000  50219500  112.592660 2015-09-25
2015-09-25  116.440002  116.690002  114.019997  114.709999  56151900  112.308730 2015-09-25

关于Python - 计算不同组中的范围(最高 - 最低),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560578/

相关文章:

python - 使用 Python 更改图像中定义的颜色

python - python中&\是什么意思

python - Pandas 计算 str 系列中的频率

python - 查找缺失的日期

python - 实现抽象属性而不需要在 child 中重新定义 __init__

python - 动态拆分 DataFrame 的列并将其存储为新列

python - 根据其他列修改数据框列中的值

python - 如何通过将函数应用于(非平凡)索引来创建列?

python - 如何使用 ctypes 将 C 函数中返回的二维数组传递给 python

python - 转换为 numpy 后如何 reshape pandas 系列?