您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中实现SQL中窗口函数lag

bubuko 2022/1/25 18:59:19 python 字数 1728 阅读 956 来源 http://www.bubuko.com/infolist-5-1.html

###Python的 shift ###代码示例 #!/usr/bin/env python3 # -- coding: UTF-8 -- import os.path import pandas as pd from datetime import datetime if __name__ == ...

Python的 shift

代码示例

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import os.path
import pandas as pd
from datetime import datetime

if __name__ == "__main__":
    top_file_dir = r"C:\Users\filter"
    according_dir = [
        datetime.strptime(file_name.split("_")[-1], ‘%Y%m%d%H%M‘) for file_name in os.listdir(top_file_dir)
        if os.path.isdir(os.path.join(top_file_dir, file_name)) and file_name.startswith("test")]
    calculate_dir = sorted(according_dir, reverse=False)
    dat_time = sorted(according_dir)
    all_daytime_df = pd.DataFrame({‘day_time‘: sorted(dat_time)})
    # 窗口函数 .LEAD(col,n,DEFAULT) 用于统计窗口内往下第n行值
    # daytime_df[‘lag_day‘] = daytime_df.shift(-1).fillna(0).astype(‘‘)
    all_daytime_df[‘lag_day‘] = all_daytime_df.shift(-1)
    # pandas计算时间差
    all_daytime_df[‘interval_day‘] = (all_daytime_df[‘lag_day‘] - all_daytime_df[‘day_time‘]).dt.seconds/60
    file_start_time = all_daytime_df.loc[:, "day_time"][[0]]
    # 数据筛选
    file_filter_time = all_daytime_df[‘lag_day‘][all_daytime_df[‘interval_day‘] > 20]
    # Series 时间转字符串
    res = pd.concat([file_start_time, file_filter_time]).apply(lambda x: "test"+x.strftime("%Y%m%d%H%M")+".json")
    out_file = os.path.join(top_file_dir, "J_DAT_json_name.txt")
    res.to_csv(out_file, header=False, index=False)

Python中实现SQL中窗口函数lag

原文:https://www.cnblogs.com/ytwang/p/15066828.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶