Python中实现SQL中窗口函数lag
2021/7/27 19:06:01
本文主要是介绍Python中实现SQL中窗口函数lag,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享