How to create a cumulative sum column in python if column value is greater than other value

joseph

I am working now in getting a cumulative sum column using pandas. However, this column most include cumulative sum only if other column value is greater than other column value. Here's an example of my current data:

Index     A       B       C
0         1       20      3
1         10      15      11
2         20      12      25
3         30      18      32
4         40      32      17
5         50      12      4

Then I want to cumsum() column A if column B is greater than C, if not value is zero. Result column D in original df should look like:

Index     A       B       C      D
0         1       20      3      1
1         10      15      11     11
2         20      12      25     0
3         30      18      32     0
4         40      32      17     40
5         50      12      4      90

I appreciate any support in advance.

wwii
df = pd.DataFrame({'A': {0: 1, 1: 10, 2: 20, 3: 30, 4: 40, 5: 50},
                   'B': {0: 20, 1: 15, 2: 12, 3: 18, 4: 32, 5: 12},
                   'C': {0: 3, 1: 11, 2: 25, 3: 32, 4: 17, 5: 4}})

Make a boolean Series for your condition and identify consecutive groups of True or False

b_gt_c = df.B > df.C
groups = b_gt_c.ne(b_gt_c.shift()).cumsum()

In [107]: b_gt_c
Out[107]: 
0     True
1     True
2    False
3    False
4     True
5     True
dtype: bool

In [108]: groups
Out[108]: 
0    1
1    1
2    2
3    2
4    3
5    3
dtype: int32

Group by those groups; multiply the cumsum of each group by the condition; assign the result to the new df column.

gb = df.groupby(groups)
for k,g in gb:
    df.loc[g.index,'D'] = g['A'].cumsum() * b_gt_c[g.index]

In [109]: df
Out[109]: 
    A   B   C     D
0   1  20   3   1.0
1  10  15  11  11.0
2  20  12  25   0.0
3  30  18  32   0.0
4  40  32  17  40.0
5  50  12   4  90.0

You could skip the for loop as well :

df['G'] = np.where(df.B.gt(df.C), df.A, np.NaN)
group = df.B.gt(df.C).ne(df.B.gt(df.C).shift()).cumsum()
df['G'] = df.groupby(group).G.cumsum().fillna(0)

Identifying consecutive occurrence of values from SO Q&A: Grouping dataframe based on consecutive occurrence of values

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UPDATE column if column is greater than a value

From Dev

SUMIF Value is Greater Than Value in Previous Column

From Dev

how to sum up a column value using the other column value

From Dev

extracting maximum value of cumulative sum into a new column

From Dev

Get column names with distinct value greater than specified values python

From Dev

Get column names with distinct value greater than specified values python

From Dev

Get column names with distinct value greater than specified values python

From Dev

Realm check if value of first column greater than value of second column

From Dev

Pandas change column value if greater than len

From Dev

Occurrences of value in column x greater than single value in column y - Python

From Dev

Cumulative Column Sum To Inform Another Column Value in R

From Dev

Cumulative Column Sum To Inform Another Column Value in R

From Dev

How to Sum value according to other column value in cakephp

From Dev

How to check if one value is greater than other

From Dev

Excel: count the number of cells in a column until their sum is greater than a set value

From Dev

Postgresql: Join another column on the least value greater than left column

From Java

Highlight a cell if the value is greater than the value of the cell from the preceding column

From Dev

Python : How to rename the column name that changes with the unique value in other column?

From Dev

Group consecutive events with same atributes and calculate the cumulative value of other column

From Dev

Group consecutive events with same atributes and calculate the cumulative value of other column

From Dev

Excel - How do I create a cumulative sum column within a group?

From Dev

Excel - How do I create a cumulative sum column within a group?

From Java

Find if value in nested sublist is greater than X within Pandas Column

From Dev

Pandas, groupby where column value is greater than x

From Dev

R keep rows with at least one column greater than value

From Dev

count the number of values greater than each value in a column of an array in r

From Dev

Split Sql Row if Value of a Column is greater than 0

From Dev

if value of column 3 is greater than cap print it in tmp not working properly

From Dev

R keep rows with at least one column greater than value

Related Related

  1. 1

    UPDATE column if column is greater than a value

  2. 2

    SUMIF Value is Greater Than Value in Previous Column

  3. 3

    how to sum up a column value using the other column value

  4. 4

    extracting maximum value of cumulative sum into a new column

  5. 5

    Get column names with distinct value greater than specified values python

  6. 6

    Get column names with distinct value greater than specified values python

  7. 7

    Get column names with distinct value greater than specified values python

  8. 8

    Realm check if value of first column greater than value of second column

  9. 9

    Pandas change column value if greater than len

  10. 10

    Occurrences of value in column x greater than single value in column y - Python

  11. 11

    Cumulative Column Sum To Inform Another Column Value in R

  12. 12

    Cumulative Column Sum To Inform Another Column Value in R

  13. 13

    How to Sum value according to other column value in cakephp

  14. 14

    How to check if one value is greater than other

  15. 15

    Excel: count the number of cells in a column until their sum is greater than a set value

  16. 16

    Postgresql: Join another column on the least value greater than left column

  17. 17

    Highlight a cell if the value is greater than the value of the cell from the preceding column

  18. 18

    Python : How to rename the column name that changes with the unique value in other column?

  19. 19

    Group consecutive events with same atributes and calculate the cumulative value of other column

  20. 20

    Group consecutive events with same atributes and calculate the cumulative value of other column

  21. 21

    Excel - How do I create a cumulative sum column within a group?

  22. 22

    Excel - How do I create a cumulative sum column within a group?

  23. 23

    Find if value in nested sublist is greater than X within Pandas Column

  24. 24

    Pandas, groupby where column value is greater than x

  25. 25

    R keep rows with at least one column greater than value

  26. 26

    count the number of values greater than each value in a column of an array in r

  27. 27

    Split Sql Row if Value of a Column is greater than 0

  28. 28

    if value of column 3 is greater than cap print it in tmp not working properly

  29. 29

    R keep rows with at least one column greater than value

HotTag

Archive