Pandas - Convert Cumulative Value To Actual Value
Let's say my dataframe looks something like this: date,site,country_code,kind,ID,rank,votes,sessions,avg_score,count 2017-03-20,website1,US,0,84,226,0.0,15.0,3.370812,53.0 2017-03-
Solution 1:
Use groupby
+ diff
, the inverse of cumsum
.
cols = ['site', 'country_code', 'kind', 'ID']
df['count'] = df.groupby(cols)['count'].diff().fillna(0)
print(df['count'])
0 0.0
1 0.0
2 0.0
3 1.0
4 0.0
5 0.0
6 0.0
7 0.0
8 3.0
9 0.0
10 3.0
11 2.0
Name: count, dtype: float64
Thanks to MaxU for pointing out the error!
Post a Comment for "Pandas - Convert Cumulative Value To Actual Value"