I have a question regarding Python for syntax.

Asked 2 years ago, Updated 2 years ago, 15 views

The cumulative total value of a particular column in the data frame was added to the last column (Cumsum_d)

Then it became like the picture above (most of the values were mosaic due to circumstances)

But what I want is that the cumulative sum is initialized every time the date changes.

For example, when the date changes, as shown in the picture below,

It's not a 3402.96 value, it's starting to accumulate again at 15.86.

I think the date interval is the same as the 96th row, so we can use the numbers in the index to make a for phrase.

I don't know how to express it because my skills are still humble.

I'm looking for help from talented people.

python for-statement

2022-09-21 17:13

1 Answers

By default, groupby operations are provided.

import pandas as pd
df = pd.DataFrame({
    'date': ['2015-01-01', '2015-01-01', '2015-01-02', '2015-01-02'],
    'value': [10, 20, 30, 40]
})
df['csum'] = df.value.groupby(df.date).cumsum()

    date    value   csum
0   2015-01-01  10  10
1   2015-01-01  20  30
2   2015-01-02  30  30
3   2015-01-02  40  70


2022-09-21 17:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.