這篇文章將為大家詳細(xì)講解有關(guān)pandas中shift和diff函數(shù)的關(guān)系是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司,專注成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷推廣,域名與空間,雅安服務(wù)器托管,網(wǎng)站托管運(yùn)營(yíng)有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問(wèn)題,請(qǐng)聯(lián)系創(chuàng)新互聯(lián)。通過(guò)?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
該函數(shù)主要的功能就是使數(shù)據(jù)框中的數(shù)據(jù)移動(dòng),若freq=None時(shí),根據(jù)axis的設(shè)置,行索引數(shù)據(jù)保持不變,列索引數(shù)據(jù)可以在行上上下移動(dòng)或在列上左右移動(dòng);若行索引為時(shí)間序列,則可以設(shè)置freq參數(shù),根據(jù)periods和freq參數(shù)值組合,使行索引每次發(fā)生periods*freq偏移量滾動(dòng),列索引數(shù)據(jù)不會(huì)移動(dòng)
① 對(duì)于DataFrame的行索引是日期型,行索引發(fā)生移動(dòng),列索引數(shù)據(jù)不變
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
結(jié)論:對(duì)于時(shí)間索引而言,shift使時(shí)間索引發(fā)生移動(dòng),其他數(shù)據(jù)保存原樣,且axis設(shè)置沒(méi)有任何影響
② 對(duì)于DataFrame行索引為非時(shí)間序列,行索引數(shù)據(jù)保持不變,列索引數(shù)據(jù)發(fā)生移動(dòng)
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
通過(guò)?pandas.DataFrame.diff命令查看幫助文檔,發(fā)現(xiàn)和shift函數(shù)形式一樣
Signature: pd.DataFrame.diff(self, periods=1, axis=0) Docstring: 1st discrete difference of object
下面看看diff函數(shù)和shift函數(shù)之間的關(guān)系
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
關(guān)于pandas中shift和diff函數(shù)的關(guān)系是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:pandas中shift和diff函數(shù)的關(guān)系是什么-創(chuàng)新互聯(lián)
URL鏈接:http://www.rwnh.cn/article22/dghejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、Google、ChatGPT、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容