天天看點

python中shift函數_Pandas Shift函數的基礎入門學習筆記

Pandas Shift函數基礎

在使用Pandas的過程中,有時會遇到shift函數,今天就一起來徹底學習下。先來看看幫助文檔是怎麼說的:

>>> import pandas

>>> help(pandas.DataFrame.shift)

Help on function shift in module pandas.core.frame:

shift(self, periods=1, freq=None, axis=0)

Shift index by desired number of periods with an optional time freq

Parameters

----------

periods : int

Number of periods to move, can be positive or negative

freq : DateOffset, timedelta, or time rule string, optional

Increment to use from the tseries module or time rule (e.g. 'EOM').

See Notes.

axis : {0 or 'index', 1 or 'columns'}

Notes

-----

If freq is specified then the index values are shifted but the data

is not realigned. That is, use freq if you would like to extend the

index when shifting and preserve the original data.

Returns

-------

shifted : DataFrame

該函數主要的功能就是使資料框中的資料移動,若freq=None時,根據axis的設定,行索引資料保持不變,列索引資料可以在行上上下移動或在列上左右移動;若行索引為時間序列,則可以設定freq參數,根據periods和freq參數值組合,使行索引每次發生periods*freq偏移量滾動,列索引資料不會移動。

參數詳解:

period:表示移動的幅度,可以是正數,也可以是負數,預設值是1,1就表示移動一次,注意這裡移動的都是資料,而索引是不移動的,移動之後沒有對應值的,就指派為NaN。

freq: DateOffset, timedelta, or time rule string,可選參數,預設值為None,隻适用于時間序列,如果這個參數存在,那麼會按照參數值移動時間索引,而資料值沒有發生變化。

axis: {0, 1, ‘index', ‘columns'},表示移動的方向,如果是0或者'index'表示上下移動,如果是1或者'columns',則會左右移動。

先來看一下一些簡單的示例:

1、非時間索引下period的設定

假設存在一個DataFrame資料df:

index value1

A 0

B 1

C 2

D 3

如果執行以下代碼  df.shift()  就會變成如下:

index value1

A NaN

B 0

C 1

D 2

執行 df.shift(2) 就會得到:

index value1

A NaN

B NaN

C 0

D 1

執行 df.shift(-1) 會得到:

index value1

A 1

B 2

C 3

D NaN

注意,shift移動的是整個資料,如果df有如下多列資料:

AA BB CC DD

a 0 1 2 3

b 4 5 6 7

c 8 9 10 11

d 12 13 14 15

執行 df.shift(2) 的資料為:

AA BB CC DD

a NaN NaN NaN NaN

b NaN NaN NaN NaN

c 0.0 1.0 2.0 3.0

d 4.0 5.0 6.0 7.0

如果隻想移動df中的某一列資料,則需要這樣操作: df['DD']= df['DD'].shift(1)

執行後的資料為:

AA BB CC DD

a 0 1 2 NaN

b 4 5 6 NaN

c 8 9 10 11

d 12 13 14 15

2、時間索引下freq 參數設定

假設存在如下DataFrame的df:

df = pd.DataFrame(np.arange(16).reshape(4,4),columns=['AA','BB','CC','DD'],index =pd.date_range('2012-06-01','2012-06-04'))

AA BB CC DD

2012-06-01 0 1 2 3

2012-06-02 4 5 6 7

2012-06-03 8 9 10 11

2012-06-04 12 13 14 15

執行 df.shift(freq=datetime.timedelta(1))  後:

AA BB CC DD

2012-06-02 0 1 2 3

2012-06-03 4 5 6 7

2012-06-04 8 9 10 11

2012-06-05 12 13 14 15

執行 df.shift(freq=datetime.timedelta(-2)) 後:

AA BB CC DD

2012-05-30 0 1 2 3

2012-05-31 4 5 6 7

2012-06-01 8 9 10 11

2012-06-02 12 13 14 15

可以看到索引直接變了。

3、axis軸向設定

df = pd.DataFrame(np.arange(16).reshape(4,4),columns=['AA','BB','CC','DD'],index =['a','b','c','d'])

df

Out[1]:

AA BB CC DD

a 0 1 2 3

b 4 5 6 7

c 8 9 10 11

d 12 13 14 15

#當period為正時,預設是axis = 0軸的設定,向下移動

df.shift(2)

Out[2]:

AA BB CC DD

a NaN NaN NaN NaN

b NaN NaN NaN NaN

c 0.0 1.0 2.0 3.0

d 4.0 5.0 6.0 7.0

#當axis=1,沿水準方向進行移動,正數向右移,負數向左移

df.shift(2,axis = 1)

Out[3]:

AA BB CC DD

a NaN NaN 0.0 1.0

b NaN NaN 4.0 5.0

c NaN NaN 8.0 9.0

d NaN NaN 12.0 13.0

#當period為負時,預設是axis = 0軸的設定,向上移動

df.shift(-1)

Out[4]:

AA BB CC DD

a 4.0 5.0 6.0 7.0

b 8.0 9.0 10.0 11.0

c 12.0 13.0 14.0 15.0

d NaN NaN NaN NaN

pandas 中上下兩行相減(隔行相減) -- shift函數的使用

最近使用pandas處理資料,需求是想相鄰兩行上下相減,查API發現shift函數,很靈活,。你也可以隔任意行相減。

p['xx_1'] = p["xx"].shift(1)

上面得到的就是xx字段向下移動一行的結果,和之前相比向下移動一行,你可以設定為任意行,也可是向上向下

p['xx'] - p["xx_1"]

這就是前後兩行的內插補點,很友善,Pandas很強大

總結

以上就是這篇文章的全部内容了,希望本文的内容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對找一找教程網的支援。