how to get day to day change pandas dataframe ¨ in python

To obtain the day-to-day change of a pandas dataframe, you can use the pct_change() method:

main.py
import pandas as pd

# Creating a sample dataframe
df = pd.DataFrame({'A': [10, 15, 12, 8, 20], 'B': [5, 7, 8, 6, 5]})

# Using pct_change() method to get the day-to-day change
df_pct = df.pct_change()

print(df_pct)
217 chars
10 lines

This will output the percent change from one day to the next:

main.py
          A         B
0       NaN       NaN
1  0.500000  0.400000
2 -0.200000  0.142857
3 -0.333333 -0.250000
4  1.500000 -0.166667
132 chars
7 lines

Note that the first row will be NaN, as it does not have a previous value to compare to.

related categories

gistlibby LogSnag