diff()函数

1 diff()是将原来的数据减去移动后的数据.

在numpy和pandas中都能调用.

pandas的调用方法:

import pandas as pd
df = pd.DataFrame( {'a':[1,2,3,4,5],
                    'b':[6,7,8,9,10],
                    'c':[11,12,13,14,15]})
print(df)
# axis=0或index表示上下移动, periods表示移动的次数,为正时向下移,为负时向上移动。
print(df.diff( periods=1, axis='index'))
print(df.diff( periods=-1, axis=0))
# axis=1或columns表示左右移动,periods表示移动的次数,为正时向右移,为负时向左移动。
print(df.diff( periods=1, axis='columns'))
print(df.diff( periods=-1, axis=1))
#    a   b   c
# 0  1   6  11
# 1  2   7  12
# 2  3   8  13
# 3  4   9  14
# 4  5  10  15
#      a    b    c
# 0  NaN  NaN  NaN
# 1  1.0  1.0  1.0
# 2  1.0  1.0  1.0
# 3  1.0  1.0  1.0
# 4  1.0  1.0  1.0
#      a    b    c
# 0 -1.0 -1.0 -1.0
# 1 -1.0 -1.0 -1.0
# 2 -1.0 -1.0 -1.0
# 3 -1.0 -1.0 -1.0
# 4  NaN  NaN  NaN
#     a    b    c
# 0 NaN  5.0  5.0
# 1 NaN  5.0  5.0
# 2 NaN  5.0  5.0
# 3 NaN  5.0  5.0
# 4 NaN  5.0  5.0
#      a    b   c
# 0 -5.0 -5.0 NaN
# 1 -5.0 -5.0 NaN
# 2 -5.0 -5.0 NaN
# 3 -5.0 -5.0 NaN
# 4 -5.0 -5.0 NaN
View Code

numpy中的调用方法:

参考:https://blog.csdn.net/qq_32618817/article/details/80653841

posted on 2019-09-30 21:26  吃我一枪  阅读(4085)  评论(0编辑  收藏  举报

导航