Home > Mobile >  Why cumulative sum has a drop
Why cumulative sum has a drop

Time:11-21

I have a certain feature in my data which looks like this:

Original column values vs timestamp

I'm trying to introduce cumulative sum this column in the DataFrame as following (the feature is int64 type):

df['Cumulative'] = df['feature'].cumsum()

But for unknown reason I have a drop in this function which is weird since the min number in the original column is 0:

Cumulative feature vs timestamp

Can someone explain why this happens and how can I fix that.Because I just want to sum the feature as it appears.

Thank you in advance.

CodePudding user response:

Like in the comments suggested, sorting first and after that build the cumulative sum. Did you try it like this:

df = df.sort_values(by='Date') #where "Date" is the column name of the values on the x-axis
df['cumulative'] = df['feature'].cumsum()
  • Related