pandas or sql计算前后两行数据间的增值方法
作者:tulinying 时间:2022-08-08 09:36:30
遇到这样一个需求,有一张表,要给这张表新增一个字段delta,delta的值等于每行的c1列的值减去上一行c1列的值。
我的解决方案,可以通过python的pandas的diff来实现,也可以通过sql来实现,如下
import pandas as pd
srcTable = pd.read_csv('pos1.csv')
print(srcTable)
destTable = srcTable.loc[srcTable.tid == 1, ['ts1', 'ts2']].sort_values(by='ts1')
destTable.columns = ['deltaTs1', 'deltaTs2']
destTable = destTable.diff()
destTable = destTable.fillna(0)
destTable['delay'] = destTable['deltaTs2'] - destTable['deltaTs1']
print(destTable)
出来的效果如下:
tid ts1 ts2
0 1 1500443161000 1500443161240
1 1 1500443162000 1500443162994
2 1 1500443163000 1500443163067
3 1 1500443164000 1500443164993
deltaTs1 deltaTs2 delay
0 0.0 0.0 0.0
1 1000.0 1754.0 754.0
2 1000.0 73.0 -927.0
3 1000.0 1926.0 926.0
若是用sql语句,我用的是mysql,自己构造行号rn
mysql> select main.t_id,
main.ts1,
ifnull(main.ts1-sub.ts1,0) deltaTs1,
main.ts2,
ifnull(main.ts2-sub.ts2,0) deltaTs2 from
(SELECT t_id,ts1,ts2,(@r1 :=@r1 + 1) rn FROM pos1,(SELECT @r1 := 0) r where t_id=1 ORDER BY ts1) main
left join
(SELECT t_id,ts1,ts2,(@r2 :=@r2 + 1) rn FROM pos1,(SELECT @r2 := 0) r where t_id=1 ORDER BY ts1) sub
on main.rn-1=sub.rn;
+------+---------------+----------+---------------+----------+
| t_id | ts1 | deltaTs1 | ts2 | deltaTs2 |
+------+---------------+----------+---------------+----------+
| 1 | 1500443161000 | 0 | 1500443161240 | 0 |
| 1 | 1500443162000 | 1000 | 1500443162994 | 1754 |
| 1 | 1500443163000 | 1000 | 1500443163067 | 73 |
| 1 | 1500443164000 | 1000 | 1500443164993 | 1926 |
+------+---------------+----------+---------------+----------+
测试数据如下
pos1.csv
1,1500443161000,1500443161240
1,1500443162000,1500443162994
1,1500443163000,1500443163067
1,1500443164000,1500443164993
CREATE TABLE `pos1` (
`t_id` int(11) DEFAULT NULL,
`ts1` bigint(22) DEFAULT NULL,
`ts2` bigint(22) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO pos1 VALUES (1, 1500443161000, 1500443161240);
INSERT INTO pos1 VALUES (1, 1500443162000, 1500443162994);
INSERT INTO pos1 VALUES (1, 1500443163000, 1500443163067);
INSERT INTO pos1 VALUES (1, 1500443164000, 1500443164993);
貌似有些数据库有这种当前行减去上一行数据的函数,具体我没有研究过。有知道的朋友可以告诉我一下,我印象中像Sqlserver好像有。
来源:https://blog.csdn.net/tulinying/article/details/75381886
标签:pandas,sql,数据,增值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python+matplotlib绘制3D条形图实例代码
2022-05-03 16:36:56
![](https://img.aspxhome.com/file/2023/1/80721_0s.png)
全面了解python字符串和字典
2021-05-03 21:06:43
python pygame 愤怒的小鸟游戏示例代码
2023-11-14 17:00:48
![](https://img.aspxhome.com/file/2023/6/110026_0s.png)
Python Matplotlib绘制动图平滑曲线
2022-12-28 22:23:40
![](https://img.aspxhome.com/file/2023/0/78490_0s.png)
Python进程的通信Queue、Pipe实例分析
2021-11-22 13:50:44
![](https://img.aspxhome.com/file/2023/8/65078_0s.png)
asp 数据库连接函数代码
2011-04-04 11:08:00
浅谈Python中的可变对象和不可变对象
2021-07-17 16:58:49
Django实现简单网页弹出警告代码
2022-12-31 06:56:16
用户凭什么跟你注册?
2011-06-10 13:16:00
什么是JSON及JSON的结构
2008-06-15 07:16:00
Python 中的Selenium异常处理实例代码
2021-03-11 12:42:11
![](https://img.aspxhome.com/file/2023/1/110201_0s.png)
python groupby函数实现分组后选取最值
2022-06-02 09:27:45
![](https://img.aspxhome.com/file/2023/7/69267_0s.png)
这可能是最好玩的python GUI入门实例(推荐)
2021-02-01 15:33:42
![](https://img.aspxhome.com/file/2023/0/117410_0s.png)
Pycharm插件(Grep Console)自定义规则输出颜色日志的方法
2023-04-21 19:20:50
![](https://img.aspxhome.com/file/2023/1/109951_0s.gif)
oracle 函数
2010-07-23 13:06:00
python取均匀不重复的随机数方式
2022-07-06 15:28:02
python实现连连看游戏
2021-12-02 00:25:10
Pandas提高数据分析效率的13个技巧汇总
2022-09-19 14:23:25
django模型类中,null=True,blank=True用法说明
2022-05-29 00:47:31
![](https://img.aspxhome.com/file/2023/8/115148_0s.jpg)
Python解析pcap文件示例
2023-05-16 00:08:45