pandas.diff() method in python

The diff() method in pandas is used to find the difference between consecutive elements in a DataFrame or Series.

Here is an example of how to use the diff() method in pandas:

main.py
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 3, 5, 7, 9]})

# Calculate the difference between consecutive elements in column 'A'
df['diff'] = df['A'].diff()

print(df)
194 chars
10 lines

Output:

main.py
   A  diff
0  1   NaN
1  3   2.0
2  5   2.0
3  7   2.0
4  9   2.0
66 chars
7 lines

In the above example, we create a DataFrame with column 'A' containing values [1, 3, 5, 7, 9]. We then use the diff() method to calculate the difference between consecutive elements in column 'A' and store the result in a new column 'diff'. The first value in the 'diff' column is NaN because there is no previous value to calculate the difference with.

Note that the diff() method can also be used on a Series object in a similar way.

Keep in mind that the diff() method also has optional parameters like periods and axis that can be used to customize the behavior of the method.

related categories

gistlibby LogSnag