程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The shift () method in pandas moves data

編輯:Python

Some parameters have the following meanings :

periods: Express shift Range of movement , A positive number means to move down , Negative numbers move up , The default value is 1, After the move, there is no value, and the index is missing
freq: according to freq The parameter value is used as the interval moving time index , The data value does not change , About freq You can obtain values for reference date_range Create date range freq Parameter value table and creation example _ I am a little monster blog -CSDN Blog

>>> import pandas as pd
>>> date_index=pd.date_range('2022-01-01',periods=6)
>>> time=pd.Series(range(6),index=date_index)
>>> print(time)
2022-01-01 0
2022-01-02 1
2022-01-03 2
2022-01-04 3
2022-01-05 4
2022-01-06 5
Freq: D, dtype: int64
>>> time.shift(periods=2)# Move down the 2 Time
2022-01-01 NaN
2022-01-02 NaN
2022-01-03 0.0
2022-01-04 1.0
2022-01-05 2.0
2022-01-06 3.0
Freq: D, dtype: float64
>>> time.shift(periods=-2)# Move up 2 Time
2022-01-01 2.0
2022-01-02 3.0
2022-01-03 4.0
2022-01-04 5.0
2022-01-05 NaN
2022-01-06 NaN
Freq: D, dtype: float64
>>> time.shift(periods=-2,freq='2D')# Every two days , Move up twice , So the time index shifted for four days
2021-12-28 0
2021-12-29 1
2021-12-30 2
2021-12-31 3
2022-01-01 4
2022-01-02 5
Freq: D, dtype: int64
>>> time.shift(periods=-1,freq='2D')
2021-12-30 0
2021-12-31 1
2022-01-01 2
2022-01-02 3
2022-01-03 4
2022-01-04 5
Freq: D, dtype: int64


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved