web123456

Pandas library method

Article Directory

  • 1 Function
  • 2 Function prototypes
  • 3 Examples ①

1 functioneffect

  • Move data to the specified number of bits

2 Function prototypes

(periods=1, freq=None, axis=0)
  • 1
  1. Suppose there is nowDataFrameType data df, calling function is()
  2. periods: Type isint, indicates the stride of movement, can be positive or negative, defaultperiods=1
  3. freq: The default isNone Applicable to time series only, the time index will be moved according to the parameter value, while the data value will not change.
  4. axis: The default is 0, move from top to bottom by line.

3 Examples ①

  1. 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
  1. Data shape;
   a  b  c
0  0  9  3
1  1  8  4
2  2  7  6
3  3  6  3
4  4  5  5
5  5  4  7
6  6  3  8
7  7  2  2
8  8  1  3
9  9  0  4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  1. shiftAfterwards, the data shape: (that is, move 2 units down)
     a    b    c
0  NaN  NaN  NaN
1  NaN  NaN  NaN
2  0.0  9.0  3.0
3  1.0  8.0  4.0
4  2.0  7.0  6.0
5  3.0  6.0  3.0
6  4.0  5.0  5.0
7  5.0  4.0  7.0
8  6.0  3.0  8.0
9  7.0  2.0  2.0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  1. ()The method will not modify the original data.
   a  b  c
0  0  9  3
1  1  8  4
2  2  7  6
3  3  6  3
4  4  5  5
5  5  4  7
6  6  3  8
7  7  2  2
8  8  1  3
9  9  0  4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11