zoukankan      html  css  js  c++  java
  • pandas实现hive的lag和lead函数 以及 first_value和last_value函数

    lag和lead VS shift 

    该函数的格式如下:

    • 第一个参数为列名,
    • 第二个参数为往上第n行(可选,默认为1),
    • 第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)

    lag

    lag(字段名,N,默认值) over(partition by 分组字段 order by 排序字段 排序方式) 

    lead

    lead(字段名,N,默认值) over(partition by 分组字段 order by 排序字段 排序方式)

    案例:

    select 
      cookieid, 
      createtime, 
      url, 
      row_number() over (partition by cookieid order by createtime) as rn, 
      LAG(createtime,1,'1970-01-01 00:00:00') over (partition by cookieid order by createtime) as last_1_time, 
      LAG(createtime,2) over (partition by cookieid order by createtime) as last_2_time 
    from cookie.cookie4

    select 
      cookieid, 
      createtime, 
      url, 
      row_number() over (partition by cookieid order by createtime) as rn, 
      LEAD(createtime,1,'1970-01-01 00:00:00') over (partition by cookieid order by createtime) as next_1_time, 
      LEAD(createtime,2) over (partition by cookieid order by createtime) as next_2_time 
    from cookie.cookie4;

    窗口函数的pandas实现

    pandas中使用shift函数来实现lag/lead函数

    import pandas as pd
    df=pd.read_csv('c:/Users/WQBin/Desktop/data.csv',engine='python', names=['cookieid','createtime','url'])
    df['last_url'] = df.sort_values('createtime').groupby('cookieid')['url'].shift(1)
    df['next_url'] = df.sort_values('createtime').groupby('cookieid')['url'].shift(-1)
    df.sort_values(by=['cookieid','createtime'])

    first_value和 last_value VS first()和last()

    • FIRST_VALUE 返回组中数据窗口的第一个值
      •   FIRST_VALUE ( [scalar_expression )OVER ( [ partition_by_clause ] order_by_clause )
    • LAST_VALUE 返回组中数据窗口的最后一个值
      •    LAST_VALUE ( [scalar_expression )OVER ( [ partition_by_clause order_by_clause )
    select 
      cookieid, 
      createtime, 
      url, 
      row_number() over (partition by cookieid order by createtime) as rn, 
      first_value(url) over (partition by cookieid order by createtime) as first1 
    from cookie.cookie4;

    select 
      cookieid, 
      createtime, 
      url, 
      row_number() over (partition by cookieid order by createtime) as rn, 
      last_value(url) over (partition by cookieid order by createtime) as last1 
    from cookie.cookie4;

    窗口函数的pandas实现

    df.sort_values(['createtime'], ascending=[ 1]).groupby(['cookieid']).first()

    df.sort_values(['createtime'], ascending=[ 1]).groupby(['cookieid']).last()

  • 相关阅读:
    MySql常用日期时间查询
    微信开发中网页授权access_token与基础支持的access_token异同
    Sqlserver复杂查询
    Array排序和List排序
    关于java按位操作运算
    验证redis的快照和AOF
    线程安全 加锁机制
    Redis 与 数据库处理数据的两种模式
    redis实现点击量/浏览量
    jsp网站访问次数统计的几种方法
  • 原文地址:https://www.cnblogs.com/wqbin/p/11987346.html
Copyright © 2011-2022 走看看