【问题标题】:Pandas calculate the number of times there is X sec difference between consecutive rowsPandas 计算连续行之间存在 X 秒差异的次数
【发布时间】:2019-06-18 06:28:04
【问题描述】:

我正在尝试计算每个id, date datetime 与前一行相差 10 秒的次数。

数据

id      timestamp      datetime          date           
1       1496660340  2019-06-05 10:59:00  2019-06-05
1       1496660340  2019-06-05 10:59:10  2019-06-05 
1       1496660355  2019-06-05 10:59:40  2019-06-05 <- 30 sec diff from above, so not counted

1       1496655555  2019-06-06 11:58:00  2019-06-06     
1       1496666666  2019-06-06 11:58:10  2019-06-06     
1       1496666677  2019-06-06 11:58:20  2019-06-06 

2       1496655555  2019-06-05 11:58:00  2019-06-05     
2       1496666666  2019-06-05 11:58:10  2019-06-05     
2       1496666677  2019-06-05 11:58:20  2019-06-05     

Data columns (total 4 columns):
       id              int64
     timestamp         int64
     datetime          datetime64[ns]
      date              object

希望

id         date      num_count
1       2019-06-05      1
1       2019-06-06      2
2       2019-06-05      2

我尝试过的

# get all the time differences first
df['timediff'] = df.groupby(['id','date'])['datetime'].diff() / np.timedelta64(1, 's')

#Count the number of 10sec differences
x = pd.DataFrame(df[df['timediff']==10].groupby(['id','date'],as_index=False)['timediff'].count())

我不确定这是否是正确的方法。有人能指出我正确的方向吗?

【问题讨论】:

    标签: python pandas datetime pandas-groupby


    【解决方案1】:

    您可以通过groupby 使用自定义函数:

    def difference_condition(x):
        return x.diff().dt.total_seconds().eq(10).sum()
    
    res = df.groupby(['id', 'date'])['datetime'].apply(difference_condition)
    
    print(res.reset_index(name='count'))
    
       id       date  count
    0   1 2019-06-05      1
    1   1 2019-06-06      2
    2   2 2019-06-05      2
    

    设置

    from io import StringIO
    
    x = """id|timestamp|datetime|date
    1       |1496660340  |2019-06-05 10:59:00  |2019-06-05
    1       |1496660340  |2019-06-05 10:59:10  |2019-06-05 
    1       |1496660355  |2019-06-05 10:59:40  |2019-06-05
    1       |1496655555  |2019-06-06 11:58:00  |2019-06-06     
    1       |1496666666  |2019-06-06 11:58:10  |2019-06-06     
    1       |1496666677  |2019-06-06 11:58:20  |2019-06-06 
    2       |1496655555  |2019-06-05 11:58:00  |2019-06-05     
    2       |1496666666  |2019-06-05 11:58:10  |2019-06-05     
    2       |1496666677  |2019-06-05 11:58:20  |2019-06-05"""
    
    df = pd.read_csv(StringIO(x), sep='|')
    df[['datetime', 'date']] = df[['datetime', 'date']].apply(pd.to_datetime)
    

    【讨论】:

    • 我的计数为零,您是否要在第三行的函数difference_condition(x) 中添加一个参数?
    • 你的数据类型和我的一样吗?我已经更新了我的问题以反映我的 dtype。
    • @jxn,我添加了我的设置,您可以复制粘贴确认。
    • 不知何故,只有在我修改您的函数以将函数填充为零并将浮点数转换为整数之后,我才能得到计数。 def difference_condition(x): return x.diff().dt.total_seconds().fillna(0).astype(int).eq(10).sum() 因为dt.total_seconds() 将我的数据转换为浮点数
    • 是的,当我测试时,您的方法运行良好。很奇怪:S 感谢您的帮助!
    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 2023-03-19
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多