1. <tfoot id='tcFm3'></tfoot>

    1. <small id='tcFm3'></small><noframes id='tcFm3'>

      <legend id='tcFm3'><style id='tcFm3'><dir id='tcFm3'><q id='tcFm3'></q></dir></style></legend>
        <bdo id='tcFm3'></bdo><ul id='tcFm3'></ul>
      <i id='tcFm3'><tr id='tcFm3'><dt id='tcFm3'><q id='tcFm3'><span id='tcFm3'><b id='tcFm3'><form id='tcFm3'><ins id='tcFm3'></ins><ul id='tcFm3'></ul><sub id='tcFm3'></sub></form><legend id='tcFm3'></legend><bdo id='tcFm3'><pre id='tcFm3'><center id='tcFm3'></center></pre></bdo></b><th id='tcFm3'></th></span></q></dt></tr></i><div id='tcFm3'><tfoot id='tcFm3'></tfoot><dl id='tcFm3'><fieldset id='tcFm3'></fieldset></dl></div>

    2. pandas 上一组最小/最大值

      时间:2024-08-21

    3. <small id='DDfqm'></small><noframes id='DDfqm'>

      <legend id='DDfqm'><style id='DDfqm'><dir id='DDfqm'><q id='DDfqm'></q></dir></style></legend>
        • <tfoot id='DDfqm'></tfoot>
            <bdo id='DDfqm'></bdo><ul id='DDfqm'></ul>

              1. <i id='DDfqm'><tr id='DDfqm'><dt id='DDfqm'><q id='DDfqm'><span id='DDfqm'><b id='DDfqm'><form id='DDfqm'><ins id='DDfqm'></ins><ul id='DDfqm'></ul><sub id='DDfqm'></sub></form><legend id='DDfqm'></legend><bdo id='DDfqm'><pre id='DDfqm'><center id='DDfqm'></center></pre></bdo></b><th id='DDfqm'></th></span></q></dt></tr></i><div id='DDfqm'><tfoot id='DDfqm'></tfoot><dl id='DDfqm'><fieldset id='DDfqm'></fieldset></dl></div>
                  <tbody id='DDfqm'></tbody>
                本文介绍了 pandas 上一组最小/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在Pandas中,我有如下数据集:

                                     Value
                2005-08-03 23:15:00   10.5
                2005-08-03 23:30:00   10.0
                2005-08-03 23:45:00   10.0
                2005-08-04 00:00:00   10.5
                2005-08-04 00:15:00   10.5
                2005-08-04 00:30:00   11.0
                2005-08-04 00:45:00   10.5
                2005-08-04 01:00:00   11.0
                ...
                2005-08-04 23:15:00   14.0
                2005-08-04 23:30:00   13.5
                2005-08-04 23:45:00   13.0
                2005-08-05 00:00:00   13.5
                2005-08-05 00:15:00   14.0
                2005-08-05 00:30:00   14.0
                2005-08-05 00:45:00   14.5
                

                首先,我希望按日期对数据进行分组,并将每个组的最大值存储在新列中,为此我使用了以下代码:

                df['ValueMaxInGroup'] = df.groupby(pd.TimeGrouper('D'))['Value'].transform(max)
                

                现在我想创建另一个列来存储以前的组最大值,因此所需的数据框将如下所示:

                                     Value  ValueMaxInGroup  ValueMaxInPrevGroup
                2005-08-03 23:15:00   10.5             10.5                  NaN
                2005-08-03 23:30:00   10.0             10.5                  NaN
                2005-08-03 23:45:00   10.0             10.5                  NaN
                2005-08-04 00:00:00   10.5             14.0                 10.5
                2005-08-04 00:15:00   10.5             14.0                 10.5
                2005-08-04 00:30:00   11.0             14.0                 10.5
                2005-08-04 00:45:00   10.5             14.0                 10.5
                2005-08-04 01:00:00   11.0             14.0                 10.5
                ...
                2005-08-04 23:15:00   14.0             14.0                 10.5
                2005-08-04 23:30:00   13.5             14.0                 10.5
                2005-08-04 23:45:00   13.0             14.0                 10.5
                2005-08-05 00:00:00   13.5             14.5                 14.0
                2005-08-05 00:15:00   14.0             14.5                 14.0
                2005-08-05 00:30:00   14.0             14.5                 14.0
                2005-08-05 00:45:00   14.5             14.5                 14.0
                

                因此,为了简单地获取前一行的值,我使用了

                df['ValueInPrevRow'] = df.shift(1)['Value']
                

                有没有办法获得另一组的最小/最大/f(X)?我假设

                df['ValueMaxInPrevGroup'] = df.groupby(pd.TimeGrouper('D')).shift(1)['Value'].transform(max)
                

                但是没有起作用。

                推荐答案

                使用groupby/aggshiftmerge可以得到想要的结果:

                import numpy as np
                import pandas as pd
                df = pd.DataFrame({'Value': [10.5, 10.0, 10.0, 10.5, 10.5, 11.0, 10.5, 11.0, 14.0, 13.5, 13.0, 13.5, 14.0, 14.0, 14.5]}, index=['2005-08-03 23:15:00', '2005-08-03 23:30:00', '2005-08-03 23:45:00', '2005-08-04 00:00:00', '2005-08-04 00:15:00', '2005-08-04 00:30:00', '2005-08-04 00:45:00', '2005-08-04 01:00:00', '2005-08-04 23:15:00', '2005-08-04 23:30:00', '2005-08-04 23:45:00', '2005-08-05 00:00:00', '2005-08-05 00:15:00', '2005-08-05 00:30:00', '2005-08-05 00:45:00']) 
                df.index = pd.DatetimeIndex(df.index)
                
                # This is equivalent to
                # df['group'] = pd.to_datetime(df.index.date)
                # when freq='D', but the version below works with any freq string, not just `'D'`.
                grouped = df.groupby(pd.TimeGrouper('D'))
                labels, uniqs, ngroups = grouped.grouper.group_info
                df['group'] = grouped.grouper.binlabels[labels]
                
                result = grouped[['Value']].agg(max)
                result = result.rename(columns={'Value':'Max'})
                result['PreviouMax'] = result['Max'].shift(1)
                
                df = pd.merge(df, result, left_on=['group'], right_index=True)
                print(df)
                

                收益率

                                     Value      group   Max  PreviouMax
                2005-08-03 23:15:00   10.5 2005-08-03  10.5         NaN
                2005-08-03 23:30:00   10.0 2005-08-03  10.5         NaN
                2005-08-03 23:45:00   10.0 2005-08-03  10.5         NaN
                2005-08-04 00:00:00   10.5 2005-08-04  14.0        10.5
                2005-08-04 00:15:00   10.5 2005-08-04  14.0        10.5
                2005-08-04 00:30:00   11.0 2005-08-04  14.0        10.5
                2005-08-04 00:45:00   10.5 2005-08-04  14.0        10.5
                2005-08-04 01:00:00   11.0 2005-08-04  14.0        10.5
                2005-08-04 23:15:00   14.0 2005-08-04  14.0        10.5
                2005-08-04 23:30:00   13.5 2005-08-04  14.0        10.5
                2005-08-04 23:45:00   13.0 2005-08-04  14.0        10.5
                2005-08-05 00:00:00   13.5 2005-08-05  14.5        14.0
                2005-08-05 00:15:00   14.0 2005-08-05  14.5        14.0
                2005-08-05 00:30:00   14.0 2005-08-05  14.5        14.0
                2005-08-05 00:45:00   14.5 2005-08-05  14.5        14.0
                

                这里的主要思想是用groupby/agg代替groupby/transform,这样我们就可以获得

                result = grouped[['Value']].agg(max)
                result = result.rename(columns={'Value':'Max'})
                result['PreviouMax'] = result['Max'].shift(1)
                #              Max  PreviouMax
                # group                       
                # 2005-08-03  10.5         NaN
                # 2005-08-04  14.0        10.5
                # 2005-08-05  14.5        14.0
                
                然后,所需的DataFrame可以表示为与合并的结果 resultgroup日期。

                这篇关于 pandas 上一组最小/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何按组绘制TimeSeries DataFrame,并根据条件改变线型? 下一篇:如何在具有500万行和50万组的Dask数据帧上加速groupby().sum()?

                相关文章

                  <small id='fyXx4'></small><noframes id='fyXx4'>

                  <i id='fyXx4'><tr id='fyXx4'><dt id='fyXx4'><q id='fyXx4'><span id='fyXx4'><b id='fyXx4'><form id='fyXx4'><ins id='fyXx4'></ins><ul id='fyXx4'></ul><sub id='fyXx4'></sub></form><legend id='fyXx4'></legend><bdo id='fyXx4'><pre id='fyXx4'><center id='fyXx4'></center></pre></bdo></b><th id='fyXx4'></th></span></q></dt></tr></i><div id='fyXx4'><tfoot id='fyXx4'></tfoot><dl id='fyXx4'><fieldset id='fyXx4'></fieldset></dl></div>

                    <bdo id='fyXx4'></bdo><ul id='fyXx4'></ul>
                  <tfoot id='fyXx4'></tfoot>
                  1. <legend id='fyXx4'><style id='fyXx4'><dir id='fyXx4'><q id='fyXx4'></q></dir></style></legend>