I have a dataframe that includes the total number of products made in a factory for every day but it is a cumulative field and not daily values. I am trying to calculate daily values by just subtracting every day's cumulative number from next day's number. Here is the code I'm using. I am using loc
to make sure that it inserts the new values in the original dataframe. This way it takes 10 seconds for 1000 rows which is kind of long, since the original data is much larger. Wondering if there is a faster way.
before:
date sum
0 2020-03-24 10
1 2020-03-25 50
2 2020-03-26 90
3 2020-03-27 140
4 2020-03-28 180
code:
for i in range(1, 1000):
data.loc[i, 'daily_products'] = data.loc[i, 'sum'] - data.loc[i-1, 'sum']
after:
date sum daily_products
0 2020-03-24 10
1 2020-03-25 50 40
2 2020-03-26 90 40
3 2020-03-27 140 50
4 2020-03-28 180 40
and the time it takes for 1000 rows:
Total runtime of the program is 9.468996286392212