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.
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)
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.
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)