Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.diff#

DataFrame.diff(periods=1,axis=0)[source]#

First discrete difference of element.

Calculates the difference of a DataFrame element compared with anotherelement in the DataFrame (default is element in previous row).

Parameters:
periodsint, default 1

Periods to shift for calculating difference, accepts negativevalues.

axis{0 or ‘index’, 1 or ‘columns’}, default 0

Take difference over rows (0) or columns (1).

Returns:
DataFrame

First differences of the Series.

See also

DataFrame.pct_change

Percent change over given number of periods.

DataFrame.shift

Shift index by desired number of periods with an optional time freq.

Series.diff

First discrete difference of object.

Notes

For boolean dtypes, this usesoperator.xor() rather thanoperator.sub().The result is calculated according to current dtype in DataFrame,however dtype of the result is always float64.

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   c0  1  1   11  2  1   42  3  2   93  4  3  164  5  5  255  6  8  36
>>>df.diff()     a    b     c0  NaN  NaN   NaN1  1.0  0.0   3.02  1.0  1.0   5.03  1.0  1.0   7.04  1.0  2.0   9.05  1.0  3.0  11.0

Difference with previous column

>>>df.diff(axis=1)    a  b   c0 NaN  0   01 NaN -1   32 NaN -1   73 NaN -1  134 NaN  0  205 NaN  2  28

Difference with 3rd previous row

>>>df.diff(periods=3)     a    b     c0  NaN  NaN   NaN1  NaN  NaN   NaN2  NaN  NaN   NaN3  3.0  2.0  15.04  3.0  4.0  21.05  3.0  6.0  27.0

Difference with following row

>>>df.diff(periods=-1)     a    b     c0 -1.0  0.0  -3.01 -1.0 -1.0  -5.02 -1.0 -1.0  -7.03 -1.0 -2.0  -9.04 -1.0 -3.0 -11.05  NaN  NaN   NaN

Overflow in input dtype

>>>df=pd.DataFrame({'a':[1,0]},dtype=np.uint8)>>>df.diff()       a0    NaN1  255.0

[8]ページ先頭

©2009-2025 Movatter.jp