Suppose there is nowDataFrameType data df, calling function is()
periods: Type isint, indicates the stride of movement, can be positive or negative, defaultperiods=1
freq: The default isNoneApplicable to time series only, the time index will be moved according to the parameter value, while the data value will not change.
axis: The default is 0, move from top to bottom by line.
3 Examples ①
Code
import pandas as pd
data1 = pd.DataFrame({'a':[0,1,2,3,4,5,6,7,8,9],'b':[9,8,7,6,5,4,3,2,1,0],'c':[3,4,6,3,5,7,8,2,3,4]})print(data1)
data2 = data1.shift(2)print(data2)print(data1)
1
2
3
4
5
6
7
8
9
10
11
12
Data shape;
a b c
0093118422763363445555476638772288139904
1
2
3
4
5
6
7
8
9
10
11
shiftAfterwards, the data shape: (that is, move 2 units down)
a b c
0 NaN NaN NaN
1 NaN NaN NaN
20.09.03.031.08.04.042.07.06.053.06.03.064.05.05.075.04.07.086.03.08.097.02.02.0