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

目录

  • 语法
  • 示例
在python数据分析中,可以使用shift()方法对DataFrame对象的数据进行位置的前滞、后滞移动。

语法
DataFrame.shift(periods=1, freq=None, axis=0)
  • periods可以理解为移动幅度的次数,shift默认一次移动1个单位,也默认移动1次(periods默认为1),则移动的长度为1 * periods。
  • periods可以是正数,也可以是负数。负数表示前滞,正数表示后滞。
  • freq是一个可选参数,默认为None,可以设为一个timedelta对象。适用于索引为时间序列数据时。
  • freq为None时,移动的是其他数据的值,即移动periods*1个单位长度。
  • freq部位None时,移动的是时间序列索引的值,移动的长度为periods * freq个单位长度。
  • axis默认为0,表示对列操作。如果为行则表示对行操作。
移动滞后没有对应值的默认为NaN。

示例 period为正,无freq
import pandas as pdpd.set_option('display.unicode.east_asian_width', True)data = https://www.it610.com/article/[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=https://www.it610.com/article/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)

python|python DataFrame的shift()方法的使用
文章图片

period为负,无freq
import pandas as pdpd.set_option('display.unicode.east_asian_width', True)data = https://www.it610.com/article/[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=https://www.it610.com/article/data, index=index, columns=['昨收'])print(df)print("=========================================")df['close'] = df['昨收'].shift(-1)df['change'] = df['昨收'].shift(-1) - df['close']print(df)

python|python DataFrame的shift()方法的使用
文章图片

period为正,freq为正
import pandas as pdimport datetimepd.set_option('display.unicode.east_asian_width', True)data = https://www.it610.com/article/[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=https://www.it610.com/article/data, index=index, columns=['close'])print(df)print("=========================================")print(df.shift(periods=2, freq=datetime.timedelta(3)))

如图,索引列的时间序列数据滞后了6天。(二乘以三)
python|python DataFrame的shift()方法的使用
文章图片

period为正,freq为负
import pandas as pdimport datetimepd.set_option('display.unicode.east_asian_width', True)data = https://www.it610.com/article/[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=https://www.it610.com/article/data, index=index, columns=['close'])print(df)print("=========================================")print(df.shift(periods=3, freq=datetime.timedelta(-3)))

如图,索引列的时间序列数据前滞了9天(三乘以负三)
python|python DataFrame的shift()方法的使用
文章图片

period为负,freq为负
import pandas as pdimport datetimepd.set_option('display.unicode.east_asian_width', True)data = https://www.it610.com/article/[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=https://www.it610.com/article/data, index=index, columns=['close'])print(df)print("=========================================")print(df.shift(periods=-3, freq=datetime.timedelta(-3)))

如图,索引列的时间序列数据滞后了9天(负三乘以负三)
python|python DataFrame的shift()方法的使用
文章图片

【python|python DataFrame的shift()方法的使用】 到此这篇关于python DataFrame的shift()方法的使用的文章就介绍到这了,更多相关python DataFrame shift() 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读