gpt4 book ai didi

python - 根据列的值连续性,按行拆分/分组 pandas DataFrame

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:11 32 4
gpt4 key购买 nike

如果价格超过移动平均线(由“趋势”列中的变化给出),我需要一些帮助来创建不同的行组。我将通过例子来解释它。以下是我的数据:

            close        avg      diff  trend
date
2017-02-22 13.78 13.578652 0.201348 1
2017-02-23 13.80 13.580854 0.219146 1
2017-02-24 13.67 13.581741 0.088259 1
2017-03-01 13.65 13.582421 0.067579 1
2017-03-02 13.67 13.583292 0.086708 1
2017-03-03 13.60 13.583458 0.016542 1
2017-03-06 13.40 13.581633 -0.181633 -1
2017-03-07 13.48 13.580621 -0.100621 -1
2017-03-08 13.25 13.577332 -0.327332 -1
2017-03-09 12.95 13.571090 -0.621090 -1
2017-03-10 13.40 13.569387 -0.169387 -1
2017-03-13 13.35 13.567204 -0.217204 -1
2017-03-14 13.19 13.563451 -0.373451 -1
2017-03-15 13.85 13.566302 0.283698 1
2017-03-16 13.91 13.569722 0.340278 1
2017-03-17 13.40 13.568033 -0.168033 -1
2017-03-21 13.19 13.565079 -0.375079 -1
2017-03-22 12.95 13.558959 -0.608959 -1
2017-03-23 12.82 13.551606 -0.731606 -1
2017-03-24 12.93 13.545421 -0.615421 -1
2017-03-27 12.90 13.538999 -0.638999 -1
2017-03-28 13.20 13.535626 -0.335626 -1
2017-03-29 13.22 13.532485 -0.312485 -1
2017-03-30 13.20 13.529177 -0.329177 -1
2017-03-31 13.28 13.526698 -0.246698 -1
2017-04-03 13.27 13.524143 -0.254143 -1
2017-04-04 13.20 13.520918 -0.320918 -1
2017-04-05 13.30 13.518720 -0.218720 -1
2017-04-06 13.73 13.520822 0.209178 1
2017-04-07 13.61 13.521710 0.088290 1
2017-04-10 13.78 13.524280 0.255720 1
2017-04-11 13.66 13.525630 0.134370 1

当趋势列等于 1 时我想要不同的组,趋势列等于 -1 时不同的组。像这样:

            close        avg      diff  trend
date
------- GROUP 1 for trend == 1: --------
2017-02-22 13.78 13.578652 0.201348 1
2017-02-23 13.80 13.580854 0.219146 1
2017-02-24 13.67 13.581741 0.088259 1
2017-03-01 13.65 13.582421 0.067579 1
2017-03-02 13.67 13.583292 0.086708 1
2017-03-03 13.60 13.583458 0.016542 1
------- GROUP 2 for trend == 1: --------
2017-03-15 13.85 13.566302 0.283698 1
2017-03-16 13.91 13.569722 0.340278 1
------- GROUP 3 for trend == 1: --------
2017-04-06 13.73 13.520822 0.209178 1
2017-04-07 13.61 13.521710 0.088290 1
2017-04-10 13.78 13.524280 0.255720 1
2017-04-11 13.66 13.525630 0.134370 1

趋势相同 == -1:

            close        avg      diff  trend
date
------- GROUP 1 for trend == -1: --------
2017-03-06 13.40 13.581633 -0.181633 -1
2017-03-07 13.48 13.580621 -0.100621 -1
2017-03-08 13.25 13.577332 -0.327332 -1
2017-03-09 12.95 13.571090 -0.621090 -1
2017-03-10 13.40 13.569387 -0.169387 -1
2017-03-13 13.35 13.567204 -0.217204 -1
2017-03-14 13.19 13.563451 -0.373451 -1
------- GROUP 2 for trend == -1: --------
2017-03-17 13.40 13.568033 -0.168033 -1
2017-03-21 13.19 13.565079 -0.375079 -1
2017-03-22 12.95 13.558959 -0.608959 -1
2017-03-23 12.82 13.551606 -0.731606 -1
2017-03-24 12.93 13.545421 -0.615421 -1
2017-03-27 12.90 13.538999 -0.638999 -1
2017-03-28 13.20 13.535626 -0.335626 -1
2017-03-29 13.22 13.532485 -0.312485 -1
2017-03-30 13.20 13.529177 -0.329177 -1
2017-03-31 13.28 13.526698 -0.246698 -1
2017-04-03 13.27 13.524143 -0.254143 -1
2017-04-04 13.20 13.520918 -0.320918 -1
2017-04-05 13.30 13.518720 -0.218720 -1

任何有关如何实现该目标的提示都将受到欢迎。如果有程序解决方案(不使用循环),我会很高兴。

最佳答案

使用 shift()cumsum() 以及 bool 值等于 1 的事实,您可以构建一个检查连续性的组:

代码:

df.groupby((df.trend != df.trend.shift()).cumsum())

测试代码:

df = pd.read_fwf(StringIO(u"""
date close avg diff trend
2017-03-01 13.65 13.582421 0.067579 1
2017-03-02 13.67 13.583292 0.086708 1
2017-03-03 13.60 13.583458 0.016542 1
2017-03-06 13.40 13.581633 -0.181633 -1
2017-03-07 13.48 13.580621 -0.100621 -1
2017-03-08 13.25 13.577332 -0.327332 -1
2017-03-09 12.95 13.571090 -0.621090 1
2017-03-10 13.40 13.569387 -0.169387 1
2017-03-13 13.35 13.567204 -0.217204 -1
2017-03-14 13.19 13.563451 -0.373451 -1
2017-03-15 13.85 13.566302 0.283698 1
2017-03-16 13.91 13.569722 0.340278 1
2017-03-17 13.40 13.568033 -0.168033 1"""),
header=1).set_index(['date'])

for group in df.groupby((df.trend != df.trend.shift()).cumsum()):
print(group)

结果:

(1,             close        avg      diff  trend
date
2017-03-01 13.65 13.582421 0.067579 1
2017-03-02 13.67 13.583292 0.086708 1
2017-03-03 13.60 13.583458 0.016542 1)
(2, close avg diff trend
date
2017-03-06 13.40 13.581633 -0.181633 -1
2017-03-07 13.48 13.580621 -0.100621 -1
2017-03-08 13.25 13.577332 -0.327332 -1)
(3, close avg diff trend
date
2017-03-09 12.95 13.571090 -0.621090 1
2017-03-10 13.40 13.569387 -0.169387 1)
(4, close avg diff trend
date
2017-03-13 13.35 13.567204 -0.217204 -1
2017-03-14 13.19 13.563451 -0.373451 -1)
(5, close avg diff trend
date
2017-03-15 13.85 13.566302 0.283698 1
2017-03-16 13.91 13.569722 0.340278 1
2017-03-17 13.40 13.568033 -0.168033 1)

关于python - 根据列的值连续性,按行拆分/分组 pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43549658/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com