当前位置:   article > 正文

pandas移动操作shift函数_df.shift

df.shift

shift 概念

shift函数是在不改变索引情况下对数据进行移动的操作,pandas 中上下两行相减(隔行相减) -- shift函数的使用

  1. df.shift(periods=1, freq=None, axis=0, fill_value=None)
  2. """
  3. periods: int 要移动的值,默认1,可不写periods
  4. frep: 只适用于时间序列,DateOffset, timedelta, or time rule string,可选参数,
  5. 默认值为None,一般不用
  6. 如果这个参数存在,那么会按照参数值移动时间索引,而数据值没有发生变化。
  7. axis:指定要移动的行或列,0为行,1为列, 上下左右移动
  8. fill_value:指定移位后的缺失数据填充值
  9. periods : int
  10. Number of periods to move, can be positive or negative
  11. freq : DateOffset, timedelta, or time rule string, optional
  12. Increment to use from the tseries module or time rule (e.g. 'EOM').
  13. See Notes.
  14. axis : {0 or 'index', 1 or 'columns'}
  15. """

源码 

  1. help(pandas.DataFrame.shift)
  2. shift(self, periods=1, freq=None, axis=0, fill_value=None) -> 'DataFrame'
  3. Shift index by desired number of periods with an optional time `freq`.
  4. When `freq` is not passed, shift the index without realigning the data.
  5. If `freq` is passed (in this case, the index must be date or datetime,
  6. or it will raise a `NotImplementedError`), the index will be
  7. increased using the periods and the `freq`. `freq` can be inferred
  8. when specified as "infer" as long as either freq or inferred_freq
  9. attribute is set in the index.
  10. Parameters
  11. ----------
  12. periods : int
  13. Number of periods to shift. Can be positive or negative.
  14. freq : DateOffset, tseries.offsets, timedelta, or str, optional
  15. Offset to use from the tseries module or time rule (e.g. 'EOM').
  16. If `freq` is specified then the index values are shifted but the
  17. data is not realigned. That is, use `freq` if you would like to
  18. extend the index when shifting and preserve the original data.
  19. If `freq` is specified as "infer" then it will be inferred from
  20. the freq or inferred_freq attributes of the index. If neither of
  21. those attributes exist, a ValueError is thrown
  22. axis : {0 or 'index', 1 or 'columns', None}, default None
  23. Shift direction.
  24. fill_value : object, optional
  25. The scalar value to use for newly introduced missing values.
  26. the default depends on the dtype of `self`.
  27. For numeric data, ``np.nan`` is used.
  28. For datetime, timedelta, or period data, etc. :attr:`NaT` is used.
  29. For extension dtypes, ``self.dtype.na_value`` is used.
  30. .. versionchanged:: 1.1.0
  31. Returns
  32. -------
  33. DataFrame
  34. Copy of input object, shifted.
  35. See Also
  36. --------
  37. Index.shift : Shift values of Index.
  38. DatetimeIndex.shift : Shift values of DatetimeIndex.
  39. PeriodIndex.shift : Shift values of PeriodIndex.
  40. tshift : Shift the time index, using the index's frequency if
  41. available.

实例

  1. Examples
  2. --------
  3. >>> df = pd.DataFrame({"Col1": [10, 20, 15, 30, 45],
  4. ... "Col2": [13, 23, 18, 33, 48],
  5. ... "Col3": [17, 27, 22, 37, 52]},
  6. ... index=pd.date_range("2020-01-01", "2020-01-05"))
  7. >>> df
  8. Col1 Col2 Col3
  9. 2020-01-01 10 13 17
  10. 2020-01-02 20 23 27
  11. 2020-01-03 15 18 22
  12. 2020-01-04 30 33 37
  13. 2020-01-05 45 48 52
  14. >>> df.shift(periods=3)
  15. Col1 Col2 Col3
  16. 2020-01-01 NaN NaN NaN
  17. 2020-01-02 NaN NaN NaN
  18. 2020-01-03 NaN NaN NaN
  19. 2020-01-04 10.0 13.0 17.0
  20. 2020-01-05 20.0 23.0 27.0
  21. >>> df.shift(periods=1, axis="columns")
  22. Col1 Col2 Col3
  23. 2020-01-01 NaN 10.0 13.0
  24. 2020-01-02 NaN 20.0 23.0
  25. 2020-01-03 NaN 15.0 18.0
  26. 2020-01-04 NaN 30.0 33.0
  27. 2020-01-05 NaN 45.0 48.0
  28. >>> df.shift(periods=3, fill_value=0)
  29. Col1 Col2 Col3
  30. 2020-01-01 0 0 0
  31. 2020-01-02 0 0 0
  32. 2020-01-03 0 0 0
  33. 2020-01-04 10 13 17
  34. 2020-01-05 20 23 27
  35. >>> df.shift(periods=3, freq="D")
  36. Col1 Col2 Col3
  37. 2020-01-04 10 13 17
  38. 2020-01-05 20 23 27
  39. 2020-01-06 15 18 22
  40. 2020-01-07 30 33 37
  41. 2020-01-08 45 48 52
  42. >>> df.shift(periods=3, freq="infer")
  43. Col1 Col2 Col3
  44. 2020-01-04 10 13 17
  45. 2020-01-05 20 23 27
  46. 2020-01-06 15 18 22
  47. 2020-01-07 30 33 37
  48. 2020-01-08 45 48 52

freq参数

  1. data = {
  2. "A":[1,2,3,0],
  3. "B":[4,5,6,0],
  4. "C":[7,8,9,0],
  5. "D":[7,8,9,0],
  6. }
  7. # df = pd.DataFrame(data)
  8. df = pd.DataFrame(data,index =pd.date_range('2012-06-01','2012-06-04'))
  9. df2= df.shift(freq=datetime.timedelta(1))
  10. df3 =df.shift(freq=datetime.timedelta(-2))
  11. print(df)
  12. print(df2)
  13. print(df3)
  14. # 只改索引不改值
  15. """
  16. A B C D
  17. 2012-06-01 1 4 7 7
  18. 2012-06-02 2 5 8 8
  19. 2012-06-03 3 6 9 9
  20. 2012-06-04 0 0 0 0
  21. A B C D
  22. 2012-06-02 1 4 7 7
  23. 2012-06-03 2 5 8 8
  24. 2012-06-04 3 6 9 9
  25. 2012-06-05 0 0 0 0
  26. A B C D
  27. 2012-05-30 1 4 7 7
  28. 2012-05-31 2 5 8 8
  29. 2012-06-01 3 6 9 9
  30. 2012-06-02 0 0 0 0
  31. """

参考

Python pandas.DataFrame.shift函数方法的使用-CJavaPy

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/945400
推荐阅读
相关标签
  

闽ICP备14008679号