# How do I calculate day on day difference in a pandas dataframe

I have a custom table in the DB that stores daily balances every working day. I want to add a new column to this table called daily_difference. This is how it should be calculated:

1.) When an account appears on this table for the first time, the daily difference should be whatever balance the account closed with. 2.) If the account has already been recorded previously, then the daily_difference = closing balance for the recorded day – closing balance for the previous day (bear in mind that even when an account has a 0 balance, the account would still have a corresponding closing_balance entry).

Please see below this table to assist you on my expectations.

```data = {
'Cust_Id': ['A', 'B', 'C', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'E'],
'Report_Date': ['01/12/2020', '01/12/2020', '01/12/2020', '02/12/2020', '02/12/2020', '02/12/2020', '02/12/2020',
'03/12/2020', '03/12/2020', '03/12/2020', '03/12/2020', '04/12/2020', '04/12/2020', '04/12/2020',
'04/12/2020', '04/12/2020'],
'Closing_Balance': [10000, 0, 5000, 2000, 16000, 10000, 1500, 5000, 12000, 7500, 0, 7000, 14000, 3000, 4000,
10000],
'Daily_Difference[Target Column]': [10000, 0, 5000, -8000, 16000, 5000, 1500, 3000, -4000, -2500, -1500, 2000,
2000, -4500, 4000, 10000]
}

df = pd.DataFrame(data)

```

Please let me know if you have any questions for me. Thank you so much!

Side Note: This dataset will continue to grow in size. I hope you take this into account when proffering answers as computational speed is of great importance.

Combine `diff` with `fillna`:
```df.groupby(['Cust_Id'])['Closing_Balance'].diff().fillna(df['Closing_Balance'])