python - 如何计算 Python Pandas 中组的移位列

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

我有以下 Pandas 数据框:

    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 不同的某些行上,我不断得到错误的结果。 您能否指出一种更干净的方法来获得所需的结果?

最佳答案

使用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

关于python - 如何计算 Python Pandas 中组的移位列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54318125/

相关文章:

python - 一些很好的教程来介绍我自己进入 Telepathy API?

python - 如何设置 PyCharm 使 '^M' 不出现在 ubuntu 中

python - ZMQ DEALER ROUTER 高频丢消息?

python - 交替的 SQL 查询

json - 使用 Python 从 Json 文件中读取和写入 Unicode 字符

python - 相当于 Python 中在字符串前面添加 "r"的功能

python - 即使在 pip 安装后也找不到模块

python - 在 Python 中重构数据框

Pandas 系列 : Decrement DateTime by 100 Years

python - 创建一个零填充的 Pandas 数据框