Add new column with incremental increase

Question:

I have a dataframe that looks like this:

           Date      LOC_A 
0    2022-07-01        154          
1    2022-07-02        162          
2    2022-07-03        170          
3    2022-07-04        169          
4    2022-07-05        201  

I would like to create a new column based on the data in column LOC_A, where the new column, let’s call it VOL_SCORE, starts at 0 when LOC_A equals 160 and then increases by 1 every time the value in LOC_A increases by 1. Anything below 160 would be 0.

           Date      LOC_A   VOL_SCORE
0    2022-07-01        154       0           
1    2022-07-02        162       3        
2    2022-07-03        170      11         
3    2022-07-04        169      10         
4    2022-07-05        201      42

There’s a similar question but I’m not sure how to apply that answer to my situation since Im attempting to create a new column.

https://stackoverflow.com/questions/38862293/how-to-add-incremental-numbers-to-a-new-column-using-pandas#:~:text=For%20a%20pandas%20DataFrame%20whose%20index%20starts%20at,the%20end%3A%20df%20%5B%27New_ID%27%5D%20%3D%20df.index%20%2B%20880

Asked By: Raven

||

Answers:

You need to make a new column and then use boolean indexing and the cumsum() method for this task, like this..

import pandas as pd

# create example dataframe
df = pd.DataFrame({'Date': ['2022-07-01', '2022-07-02', '2022-07-03', '2022-07-04', '2022-07-05'], 
                   'LOC_A': [154, 162, 170, 169, 201]})

# create a new column VOL_SCORE
df['VOL_SCORE'] = (df['LOC_A'].ge(160) * (df['LOC_A'] - 160)).cumsum()

print(df)

Answered By: Beatdown

Subtract your reference with sub and clip the negative values to 0:

ref = 160
df['VOL_SCORE'] = df['LOC_A'].sub(ref-1).clip(lower=0)

Output:

         Date  LOC_A  VOL_SCORE
0  2022-07-01    154          0
1  2022-07-02    162          3
2  2022-07-03    170         11
3  2022-07-04    169         10
4  2022-07-05    201         42
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.