Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。
Pandas dataframe.shift()函数根据需要的周期数移动索引,并带有可选的时间频率。该函数采用称为周期的标量参数,该参数表示要在所需轴上进行的平移次数。处理时间序列数据时,此功能非常有用。
用法:DataFrame.shift(periods=1, freq=None, axis=0)
参数:
periods: 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’}
返回:shifted:DataFrame
范例1:采用shift()在时间序列数据中将索引轴移动2个周期的功能
# importing pandas as pd
import pandas as pd
# Creating row index values for our data frame
# We have taken time frequency to be of 12 hours interval
# We are generating five index value using "period = 5" parameter
ind &#