您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關pandas中shift和diff函數的關系是什么,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
通過?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
關于pandas中shift和diff函數的關系是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。