浅谈pandas中shift和diff函数关系
作者:每天进步一点点2017 时间:2022-11-15 08:48:42
通过?pandas.DataFrame.shift命令查看帮助文档
Signature: pandas.DataFrame.shift(self, periods=1, freq=None, axis=0)
Docstring:
Shift index by desired number of periods with an optional time freq
该函数主要的功能就是使数据框中的数据移动,若freq=None时,根据axis的设置,行索引数据保持不变,列索引数据可以在行上上下移动或在列上左右移动;若行索引为时间序列,则可以设置freq参数,根据periods和freq参数值组合,使行索引每次发生periods*freq偏移量滚动,列索引数据不会移动
① 对于DataFrame的行索引是日期型,行索引发生移动,列索引数据不变
In [2]: import pandas as pd
...: import numpy as np
...: df = pd.DataFrame(np.arange(24).reshape(6,4),index=pd.date_range(start=
...: '20170101',periods=6),columns=['A','B','C','D'])
...: df
...:
Out[2]:
A B C D
2017-01-01 0 1 2 3
2017-01-02 4 5 6 7
2017-01-03 8 9 10 11
2017-01-04 12 13 14 15
2017-01-05 16 17 18 19
2017-01-06 20 21 22 23
In [3]: df.shift(2,axis=0,freq='2D')
Out[3]:
A B C D
2017-01-05 0 1 2 3
2017-01-06 4 5 6 7
2017-01-07 8 9 10 11
2017-01-08 12 13 14 15
2017-01-09 16 17 18 19
2017-01-10 20 21 22 23
In [4]: df.shift(2,axis=1,freq='2D')
Out[4]:
A B C D
2017-01-05 0 1 2 3
2017-01-06 4 5 6 7
2017-01-07 8 9 10 11
2017-01-08 12 13 14 15
2017-01-09 16 17 18 19
2017-01-10 20 21 22 23
In [5]: df.shift(2,freq='2D')
Out[5]:
A B C D
2017-01-05 0 1 2 3
2017-01-06 4 5 6 7
2017-01-07 8 9 10 11
2017-01-08 12 13 14 15
2017-01-09 16 17 18 19
2017-01-10 20 21 22 23
结论:对于时间索引而言,shift使时间索引发生移动,其他数据保存原样,且axis设置没有任何影响
② 对于DataFrame行索引为非时间序列,行索引数据保持不变,列索引数据发生移动
In [6]: import pandas as pd
...: import numpy as np
...: df = pd.DataFrame(np.arange(24).reshape(6,4),index=['r1','r2','r3','r4'
...: ,'r5','r6'],columns=['A','B','C','D'])
...: df
...:
Out[6]:
A B C D
r1 0 1 2 3
r2 4 5 6 7
r3 8 9 10 11
r4 12 13 14 15
r5 16 17 18 19
r6 20 21 22 23
In [7]: df.shift(periods=2,axis=0)
Out[7]:
A B C D
r1 NaN NaN NaN NaN
r2 NaN NaN NaN NaN
r3 0.0 1.0 2.0 3.0
r4 4.0 5.0 6.0 7.0
r5 8.0 9.0 10.0 11.0
r6 12.0 13.0 14.0 15.0
In [8]: df.shift(periods=-2,axis=0)
Out[8]:
A B C D
r1 8.0 9.0 10.0 11.0
r2 12.0 13.0 14.0 15.0
r3 16.0 17.0 18.0 19.0
r4 20.0 21.0 22.0 23.0
r5 NaN NaN NaN NaN
r6 NaN NaN NaN NaN
In [9]: df.shift(periods=2,axis=1)
Out[9]:
A B C D
r1 NaN NaN 0.0 1.0
r2 NaN NaN 4.0 5.0
r3 NaN NaN 8.0 9.0
r4 NaN NaN 12.0 13.0
r5 NaN NaN 16.0 17.0
r6 NaN NaN 20.0 21.0
In [10]: df.shift(periods=-2,axis=1)
Out[10]:
A B C D
r1 2.0 3.0 NaN NaN
r2 6.0 7.0 NaN NaN
r3 10.0 11.0 NaN NaN
r4 14.0 15.0 NaN NaN
r5 18.0 19.0 NaN NaN
r6 22.0 23.0 NaN NaN
通过?pandas.DataFrame.diff命令查看帮助文档,发现和shift函数形式一样
Signature: pd.DataFrame.diff(self, periods=1, axis=0)
Docstring:
1st discrete difference of object
下面看看diff函数和shift函数之间的关系
In [13]: df.diff(periods=2,axis=0)
Out[13]:
A B C D
r1 NaN NaN NaN NaN
r2 NaN NaN NaN NaN
r3 8.0 8.0 8.0 8.0
r4 8.0 8.0 8.0 8.0
r5 8.0 8.0 8.0 8.0
r6 8.0 8.0 8.0 8.0
In [14]: df -df.diff(periods=2,axis=0)
Out[14]:
A B C D
r1 NaN NaN NaN NaN
r2 NaN NaN NaN NaN
r3 0.0 1.0 2.0 3.0
r4 4.0 5.0 6.0 7.0
r5 8.0 9.0 10.0 11.0
r6 12.0 13.0 14.0 15.0
In [15]: df.shift(periods=2,axis=0)
Out[15]:
A B C D
r1 NaN NaN NaN NaN
r2 NaN NaN NaN NaN
r3 0.0 1.0 2.0 3.0
r4 4.0 5.0 6.0 7.0
r5 8.0 9.0 10.0 11.0
r6 12.0 13.0 14.0 15.0
来源:https://blog.csdn.net/kancy110/article/details/77138929
标签:pandas,diff,shift,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现FTP文件定时自动下载的步骤
2022-04-13 02:07:14
Python的词法分析与语法分析
2022-06-20 13:58:34
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
2023-09-07 18:25:35
![](https://img.aspxhome.com/file/2023/2/126912_0s.png)
sina和265天气预报调用代码
2007-11-19 13:32:00
python 使用fileinput读取文件
2021-09-07 23:48:43
![](https://img.aspxhome.com/file/2023/4/115084_0s.jpg)
JS如何实现在弹出窗口中加载页面
2024-04-29 14:07:53
![](https://img.aspxhome.com/file/2023/7/130867_0s.gif)
python查看FTP是否能连接成功的方法
2023-11-04 13:31:50
2008年Logo设计10大趋势
2008-02-28 13:06:00
![](https://img.aspxhome.com/file/UploadPic/20082/28/200822813204572s.gif)
vue中使用$http.post请求传参的错误及解决
2024-04-27 15:47:13
![](https://img.aspxhome.com/file/2023/5/133155_0s.png)
关于Javascript的内存泄漏问题
2008-04-15 07:46:00
纯JS实现动态时间显示代码
2024-05-02 17:31:34
JavaScript实现酷炫的鼠标拖尾特效
2024-06-16 16:02:25
![](https://img.aspxhome.com/file/2023/0/56540_0s.png)
Python 登录网站详解及实例
2022-05-31 00:47:57
django执行原生SQL查询的实现
2023-11-11 13:34:04
Python3 读取Word文件方式
2021-03-21 22:36:37
用Python 爬取猫眼电影数据分析《无名之辈》
2023-07-03 17:23:26
![](https://img.aspxhome.com/file/2023/5/118055_0s.png)
DHTML 打造 Picture Spelling
2013-08-22 17:01:53
重新认识ASP之后的我在想"是时候改变了"
2008-05-08 13:10:00
运行tensorflow python程序,限制对GPU和CPU的占用操作
2022-07-03 02:49:26
显示日期格式的几个函数
2008-09-03 13:20:00