📜  Python|熊猫 dataframe.shift()

📅  最后修改于: 2022-05-13 01:54:30.606000             🧑  作者: Mango

Python|熊猫 dataframe.shift()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.shift()函数将索引按所需的周期数和可选的时间频率移动。此函数采用一个称为period的标量参数,它表示要在所需轴上进行的移动次数。此函数在处理时间序列数据时非常有用。

示例 #1:使用 shift()函数将时间序列数据中的索引轴移动 2 个周期

Python3
# 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 = pd.date_range('01 / 01 / 2000', periods = 5, freq ='12H')
  
# Creating a dataframe with 4 columns
# using "ind" as the index for our dataframe
df = pd.DataFrame({"A":[1, 2, 3, 4, 5],
                   "B":[10, 20, 30, 40, 50],
                   "C":[11, 22, 33, 44, 55],
                   "D":[12, 24, 51, 36, 2]},
                    index = ind)
 
# Print the dataframe
df


Python3
# shift index axis by two periods in positive direction
# axis = 0 is set by default
df.shift(2, axis = 0)


Python3
# shift index axis by two periods in negative direction
# axis = 0 is set by default
df.shift(-2, axis = 0)


Python3
# 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 = pd.date_range('01 / 01 / 2000', periods = 5, freq ='12H')
  
# Creating a dataframe with 4 columns
# using "ind" as the index for our dataframe
df = pd.DataFrame({"A":[1, 2, 3, 4, 5],
                   "B":[10, 20, 30, 40, 50],
                   "C":[11, 22, 33, 44, 55],
                   "D":[12, 24, 51, 36, 2]},
                    index = ind)
 
# Print the dataframe
df


Python3
# shift column axis by two periods in positive direction
df.shift(2, axis = 1)


Python3
# shift column axis by two periods in negative direction
df.shift(-2, axis = 1)


输出:

让我们使用 dataframe.shift()函数将索引轴在正方向上移动 2 个周期

Python3

# shift index axis by two periods in positive direction
# axis = 0 is set by default
df.shift(2, axis = 0)

输出:

让我们将索引轴向负方向移动一些周期

Python3

# shift index axis by two periods in negative direction
# axis = 0 is set by default
df.shift(-2, axis = 0)

输出 :


示例 #2:使用 shift()函数将时间序列数据中的列轴移动 2 个周期

Python3

# 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 = pd.date_range('01 / 01 / 2000', periods = 5, freq ='12H')
  
# Creating a dataframe with 4 columns
# using "ind" as the index for our dataframe
df = pd.DataFrame({"A":[1, 2, 3, 4, 5],
                   "B":[10, 20, 30, 40, 50],
                   "C":[11, 22, 33, 44, 55],
                   "D":[12, 24, 51, 36, 2]},
                    index = ind)
 
# Print the dataframe
df

让我们使用 dataframe.shift()函数将列轴沿正方向移动 2 个周期

Python3

# shift column axis by two periods in positive direction
df.shift(2, axis = 1)

让我们将列轴向负方向移动一些周期

Python3

# shift column axis by two periods in negative direction
df.shift(-2, axis = 1)

输出 :