python?DataFrame的shift()方法的使用
在python數(shù)據(jù)分析中,可以使用shift()方法對DataFrame對象的數(shù)據(jù)進(jìn)行位置的前滯、后滯移動。
語法
DataFrame.shift(periods=1, freq=None, axis=0)
- periods可以理解為移動幅度的次數(shù),shift默認(rèn)一次移動1個(gè)單位,也默認(rèn)移動1次(periods默認(rèn)為1),則移動的長度為1 * periods。
- periods可以是正數(shù),也可以是負(fù)數(shù)。負(fù)數(shù)表示前滯,正數(shù)表示后滯。
- freq是一個(gè)可選參數(shù),默認(rèn)為None,可以設(shè)為一個(gè)timedelta對象。適用于索引為時(shí)間序列數(shù)據(jù)時(shí)。
- freq為None時(shí),移動的是其他數(shù)據(jù)的值,即移動periods*1個(gè)單位長度。
- freq部位None時(shí),移動的是時(shí)間序列索引的值,移動的長度為periods * freq個(gè)單位長度。
- axis默認(rèn)為0,表示對列操作。如果為行則表示對行操作。
移動滯后沒有對應(yīng)值的默認(rèn)為NaN。
示例
period為正,無freq
import pandas as pd
pd.set_option('display.unicode.east_asian_width', True)
data = [51.0, 52.33, 51.21, 54.23, 56.78]
index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']
df = pd.DataFrame(data=data, index=index, columns=['close'])
df.index.name = 'date'
print(df)
print("=========================================")
df['昨收'] = df['close'].shift()
df['change'] = df['close'] - df['close'].shift()
print(df)

period為負(fù),無freq
import pandas as pd
pd.set_option('display.unicode.east_asian_width', True)
data = [51.0, 52.33, 51.21, 54.23, 56.78]
index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']
index = pd.to_datetime(index)
index.name = 'date'
df = pd.DataFrame(data=data, index=index, columns=['昨收'])
print(df)
print("=========================================")
df['close'] = df['昨收'].shift(-1)
df['change'] = df['昨收'].shift(-1) - df['close']
print(df)

period為正,freq為正
import pandas as pd
import datetime
pd.set_option('display.unicode.east_asian_width', True)
data = [51.0, 52.33, 51.21, 54.23, 56.78]
index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']
index = pd.to_datetime(index)
index.name = 'date'
df = pd.DataFrame(data=data, index=index, columns=['close'])
print(df)
print("=========================================")
print(df.shift(periods=2, freq=datetime.timedelta(3)))
如圖,索引列的時(shí)間序列數(shù)據(jù)滯后了6天。(二乘以三)

period為正,freq為負(fù)
import pandas as pd
import datetime
pd.set_option('display.unicode.east_asian_width', True)
data = [51.0, 52.33, 51.21, 54.23, 56.78]
index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']
index = pd.to_datetime(index)
index.name = 'date'
df = pd.DataFrame(data=data, index=index, columns=['close'])
print(df)
print("=========================================")
print(df.shift(periods=3, freq=datetime.timedelta(-3)))
如圖,索引列的時(shí)間序列數(shù)據(jù)前滯了9天(三乘以負(fù)三)

period為負(fù),freq為負(fù)
import pandas as pd
import datetime
pd.set_option('display.unicode.east_asian_width', True)
data = [51.0, 52.33, 51.21, 54.23, 56.78]
index = ['2022-2-28', '2022-3-1', '2022-3-2', '2022-3-3', '2022-3-4']
index = pd.to_datetime(index)
index.name = 'date'
df = pd.DataFrame(data=data, index=index, columns=['close'])
print(df)
print("=========================================")
print(df.shift(periods=-3, freq=datetime.timedelta(-3)))
如圖,索引列的時(shí)間序列數(shù)據(jù)滯后了9天(負(fù)三乘以負(fù)三)

到此這篇關(guān)于python DataFrame的shift()方法的使用的文章就介紹到這了,更多相關(guān)python DataFrame shift() 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
tensorflow官方github預(yù)訓(xùn)練模型下載方式
這篇文章主要介紹了tensorflow官方github預(yù)訓(xùn)練模型下載方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
pandas實(shí)現(xiàn)to_sql將DataFrame保存到數(shù)據(jù)庫中
這篇文章主要介紹了pandas實(shí)現(xiàn)to_sql將DataFrame保存到數(shù)據(jù)庫中,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python利用wxPython實(shí)現(xiàn)ocr識別圖片漢字程序
在這篇博客中,我們將介紹一個(gè)如何使用wxPython構(gòu)建的簡單OCR識別圖片漢字應(yīng)用程序,文章的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-08-08
Python完全識別驗(yàn)證碼自動登錄實(shí)例詳解
今天小編就為大家分享一篇Python完全識別驗(yàn)證碼自動登錄實(shí)例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Python使用PyNmap進(jìn)行網(wǎng)絡(luò)掃描的詳細(xì)步驟
使用 PyNmap 進(jìn)行網(wǎng)絡(luò)掃描是一個(gè)非常有效的方式,PyNmap 是 Nmap 工具的一個(gè) Python 封裝,它允許你在 Python 腳本中使用 Nmap 的強(qiáng)大功能,本文介紹了如何使用 PyNmap 進(jìn)行網(wǎng)絡(luò)掃描的詳細(xì)步驟,需要的朋友可以參考下2024-08-08
python使用Pycharm創(chuàng)建一個(gè)Django項(xiàng)目
這篇文章主要介紹了python使用Pycharm創(chuàng)建一個(gè)Django項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Python+Mysql實(shí)現(xiàn)登錄注冊完整代碼示例
在開發(fā)中用戶注冊和登錄是常見的功能需求,這篇文章主要給大家介紹了關(guān)于Python+Mysql實(shí)現(xiàn)登錄注冊的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03

