Pandas学习之差分函数diff

栏目: 数据库 · 发布时间: 5年前

内容简介:在上一篇的文章中,我们学习了Pandas的shift函数,今天要来学习的是diff函数,shift函数与diff函数有着莫大的关联,先来看看diff函数的官方说明:从官方的说明中已经很明确的可以知道其shift函数的关系为:df.diff() = df – df.shift()diff相比shift少了一个freq参数,函数原型为:diff(self, periods=1, axis=0)

在上一篇的文章中,我们学习了Pandas的shift函数,今天要来学习的是diff函数,shift函数与diff函数有着莫大的关联,先来看看diff函数的官方说明:

>>> import pandas
>>> help(pandas.DataFrame.diff)
Help on function diff in module pandas.core.frame:
 
diff(self, periods=1, axis=0)
    First discrete difference of element.
 
    Calculates the difference of a DataFrame element compared with another
    element in the DataFrame (default is the element in the same column
    of the previous row).
 
    Parameters
    ----------
    periods : int, default 1
        Periods to shift for calculating difference, accepts negative
        values.
    axis : {0 or 'index', 1 or 'columns'}, default 0
        Take difference over rows (0) or columns (1).
 
        .. versionadded:: 0.16.1.
 
    Returns
    -------
    diffed : DataFrame
 
    See Also
    --------
    Series.diff: First discrete difference for a Series.
    DataFrame.pct_change: Percent change over given number of periods.
    DataFrame.shift: Shift index by desired number of periods with an
        optional time freq.
 
    Examples
    --------
    Difference with previous row
 
    >>> df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6],
    ...                    'b': [1, 1, 2, 3, 5, 8],
    ...                    'c': [1, 4, 9, 16, 25, 36]})
    >>> df
       a  b   c
    0  1  1   1
    1  2  1   4
    2  3  2   9
    3  4  3  16
    4  5  5  25
    5  6  8  36
 
    >>> df.diff()
         a    b     c
    0  NaN  NaN   NaN
    1  1.0  0.0   3.0
    2  1.0  1.0   5.0
    3  1.0  1.0   7.0
    4  1.0  2.0   9.0
    5  1.0  3.0  11.0
 
    Difference with previous column
 
    >>> df.diff(axis=1)
        a    b     c
    0 NaN  0.0   0.0
    1 NaN -1.0   3.0
    2 NaN -1.0   7.0
    3 NaN -1.0  13.0
    4 NaN  0.0  20.0
    5 NaN  2.0  28.0
 
    Difference with 3rd previous row
 
    >>> df.diff(periods=3)
         a    b     c
    0  NaN  NaN   NaN
    1  NaN  NaN   NaN
    2  NaN  NaN   NaN
    3  3.0  2.0  15.0
    4  3.0  4.0  21.0
    5  3.0  6.0  27.0
 
    Difference with following row
 
    >>> df.diff(periods=-1)
         a    b     c
    0 -1.0  0.0  -3.0
    1 -1.0 -1.0  -5.0
    2 -1.0 -1.0  -7.0
    3 -1.0 -2.0  -9.0
    4 -1.0 -3.0 -11.0
    5  NaN  NaN   NaN

从官方的说明中已经很明确的可以知道其shift函数的关系为:df.diff() = df – df.shift()

diff相比shift少了一个freq参数,函数原型为:diff(self, periods=1, axis=0)

其参数含义为:

  • periods:移动的幅度,int类型,默认值为1。
  • axis:移动的方向,{0 or ‘index’, 1 or ‘columns’},如果为0或者’index’,则上下移动,如果为1或者’columns’,则左右移动。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

微信小程序(开发入门及案例详解)

微信小程序(开发入门及案例详解)

李骏、边思 / 机械工业出版社 / 2017-3-1 / 59.0

本书可分为3部分,第一部分作为基础章节,介绍了第一个小程序的搭建流程,让大家能快速上手;同时对小程序框架原理进行了详细介绍,为后面学习组件、API打下基础。 第二部分对小程序组件、API进行介绍,对组件、API的使用、注意事项进行详细讲解,并给出示例代码。 最后一部分精选5个由浅入深的案例,对小程序研发进行实战讲解,涵盖了实际项目中可能涉及的技术方案和使用方法,具备很强的实战意义。 ......一起来看看 《微信小程序(开发入门及案例详解)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具