python?DataFrame的shift()方法的使用
在python數(shù)據(jù)分析中,可以使用shift()方法對DataFrame對象的數(shù)據(jù)進行位置的前滯、后滯移動。
語法
DataFrame.shift(periods=1, freq=None, axis=0)
- periods可以理解為移動幅度的次數(shù),shift默認一次移動1個單位,也默認移動1次(periods默認為1),則移動的長度為1 * periods。
- periods可以是正數(shù),也可以是負數(shù)。負數(shù)表示前滯,正數(shù)表示后滯。
- freq是一個可選參數(shù),默認為None,可以設為一個timedelta對象。適用于索引為時間序列數(shù)據(jù)時。
- freq為None時,移動的是其他數(shù)據(jù)的值,即移動periods*1個單位長度。
- freq部位None時,移動的是時間序列索引的值,移動的長度為periods * freq個單位長度。
- axis默認為0,表示對列操作。如果為行則表示對行操作。
移動滯后沒有對應值的默認為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為負,無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ù)據(jù)滯后了6天。(二乘以三)

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=3, freq=datetime.timedelta(-3)))
如圖,索引列的時間序列數(shù)據(jù)前滯了9天(三乘以負三)

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=-3, freq=datetime.timedelta(-3)))
如圖,索引列的時間序列數(shù)據(jù)滯后了9天(負三乘以負三)

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

