【问题标题】:How to calculate shifted columns over Groups in Python Pandas如何在 Python Pandas 中计算组上的移位列
【发布时间】:2019-01-22 23:52:19
【问题描述】:

我有以下熊猫数据框:

    Circuit-ID  DATETIME    LATE? 
78899   07/06/2018 15:30    1
78899   08/06/2018 17:30    0
78899   09/06/2018 20:30    1
23544   12/07/2017 23:30    1
23544   13/07/2017 19:30    0
23544   14/07/2017 20:30    1

我需要计算 DATETIME 和 LATE 的偏移值吗?列得到以下结果:

Circuit DATETIME          LATE?     DATETIME-1        LATE-1    
78899   07/06/2018 15:30    1   NA                    NA
78899   08/06/2018 17:30    0   07/06/2018 15:30       1
78899   09/06/2018 20:30    1   08/06/2018 17:30       0
23544   12/07/2017 23:30    1   NA                    NA
23544   13/07/2017 19:30    0   12/07/2017 23:30       1
23544   14/07/2017 20:30    1   13/07/2017 19:30       0

我尝试了以下代码:

df.groupby(['circuit ID, DATETILE', LATE? ]) \
            .apply(lambda x : x.sort_values(by=['circuit ID, 'DATETILE', 'LATE?'], ascending = [True, True, True]))['LATE?'] \
            .transform(lambda x:x.shift()) \
            .reset_index(name= 'LATE-1') 

但我在某些行的第一个移位值与 Nan 不同的行上不断得到错误的结果。 您能否指出一种更干净的方法来获得所需的结果?

【问题讨论】:

    标签: python python-3.x pandas pandas-groupby shift


    【解决方案1】:

    使用groupbyshift,然后重新加入:

    df.join(df.groupby('Circuit-ID').shift().add_suffix('-1'))
    
       Circuit-ID          DATETIME  LATE?        DATETIME-1  LATE?-1
    0       78899  07/06/2018 15:30      1               NaN      NaN
    1       78899  08/06/2018 17:30      0  07/06/2018 15:30      1.0
    2       78899  09/06/2018 20:30      1  08/06/2018 17:30      0.0
    3       23544  12/07/2017 23:30      1               NaN      NaN
    4       23544  13/07/2017 19:30      0  12/07/2017 23:30      1.0
    5       23544  14/07/2017 20:30      1  13/07/2017 19:30      0.0
    

    类似的解决方案使用concat 加入:

    pd.concat([df, df.groupby('Circuit-ID').shift().add_suffix('-1')], axis=1)
    
       Circuit-ID          DATETIME  LATE?        DATETIME-1  LATE?-1
    0       78899  07/06/2018 15:30      1               NaN      NaN
    1       78899  08/06/2018 17:30      0  07/06/2018 15:30      1.0
    2       78899  09/06/2018 20:30      1  08/06/2018 17:30      0.0
    3       23544  12/07/2017 23:30      1               NaN      NaN
    4       23544  13/07/2017 19:30      0  12/07/2017 23:30      1.0
    5       23544  14/07/2017 20:30      1  13/07/2017 19:30      0.0
    

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 1970-01-01
      • 2019-04-19
      • 2021-09-17
      • 2013-11-30
      • 2021-12-22
      • 2020-10-07
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多