pandas make daily deltas of a column in python

Assuming you have a dataframe df with a date column and a value column, you can use the groupby function together with diff to calculate the daily differences:

main.py
df['daily_change'] = df.groupby(['date'])['value'].diff().fillna(0)
68 chars
2 lines

This will group the dataframe by date, calculate the difference for each group (i.e. each date), and fill NaN values with 0. The daily differences will be stored in a new column called daily_change.

related categories

gistlibby LogSnag