欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python?DataFrame的shift()方法的使用

 更新時(shí)間:2022年03月10日 08:55:30   作者:侯小啾  
在python數(shù)據(jù)分析中,可以使用shift()方法對(duì)DataFrame對(duì)象的數(shù)據(jù)進(jìn)行位置的前滯、后滯移動(dòng),本文主要介紹了python?DataFrame的shift()方法的使用,感興趣的可以了解一下

在python數(shù)據(jù)分析中,可以使用shift()方法對(duì)DataFrame對(duì)象的數(shù)據(jù)進(jìn)行位置的前滯、后滯移動(dòng)。

語法

DataFrame.shift(periods=1, freq=None, axis=0)

  • periods可以理解為移動(dòng)幅度的次數(shù),shift默認(rèn)一次移動(dòng)1個(gè)單位,也默認(rèn)移動(dòng)1次(periods默認(rèn)為1),則移動(dòng)的長度為1 * periods。
  • periods可以是正數(shù),也可以是負(fù)數(shù)。負(fù)數(shù)表示前滯,正數(shù)表示后滯。
  • freq是一個(gè)可選參數(shù),默認(rèn)為None,可以設(shè)為一個(gè)timedelta對(duì)象。適用于索引為時(shí)間序列數(shù)據(jù)時(shí)。
  • freq為None時(shí),移動(dòng)的是其他數(shù)據(jù)的值,即移動(dòng)periods*1個(gè)單位長度。
  • freq部位None時(shí),移動(dòng)的是時(shí)間序列索引的值,移動(dòng)的長度為periods * freq個(gè)單位長度。
  • axis默認(rèn)為0,表示對(duì)列操作。如果為行則表示對(duì)行操作。

移動(dòng)滯后沒有對(duì)應(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論