如下所示:
>>> import pandas as pd
>>> import numpy as np
# 生成模拟数据
>>> df = pd.DataFrame({'a':np.random.randint(1, 100, 10),\
'b':np.random.randint(1, 100, 10)},\
index=map(str, range(10)))
>>> df
a b
0 21 54
1 53 28
2 18 87
3 56 40
4 62 34
5 74 10
6 7 78
7 58 79
8 66 80
9 30 21
# 纵向一阶差分,当前行减去上一行
>>> df.diff()
a b
0 NaN NaN
1 32.0 -26.0
2 -35.0 59.0
3 38.0 -47.0
4 6.0 -6.0
5 12.0 -24.0
6 -67.0 68.0
7 51.0 1.0
8 8.0 1.0
9 -36.0 -59.0
# 横向一阶差分,当前列减去左边的列
>>> df.diff(axis=1)
a b
0 NaN 33.0
<