I have this type of data, but in real life it has millions of entries. Product id is always product specific, but occurs several times during its lifetime.
| date | product id | revenue | estimated lifetime value |
|---|---|---|---|
| 2021-04-16 | 0061M00001AXc5lQAD | 970 | 2000 |
| 2021-04-17 | 0061M00001AXbCiQAL | 159 | 50000 |
| 2021-04-18 | 0061M00001AXb9AQAT | 80 | 3000 |
| 2021-04-19 | 0061M00001AXbIHQA1 | 1100 | 8000 |
| 2021-04-20 | 0061M00001AXbY8QAL | 90 | 4000 |
| 2021-04-21 | 0061M00001AXbQ1QAL | 29 | 30000 |
| 2021-04-21 | 0061M00001AXc5lQAD | 30 | 2000 |
| 2021-05-02 | 0061M00001AXc5lQAD | 50 | 2000 |
| 2021-05-05 | 0061M00001AXc5lQAD | 50 | 2000 |
I'm looking to create a new column in pandas that indicates when a certain product id has generated more revenue than a specific threshold e.g. 100$, 1000$, marking it as a Win (1). A win may occur only once during the lifecycle of a product. In addition I would want to create another column that would indicate the row where a specific product sales exceeds e.g. 10% of the estimated lifetime value.
What would be the most intuitive approach to achieve this in Python / Pandas?
edit:
dw1k_thresh: if the cumulative sales of a specific product id >= 1000, the column takes a boolean value of 1, otherwise zero. However 1 can occur only once and after that the again always zero. Basically it's just an indicator of the date and transaction when a product sales exceeds the critical value of 1000.
dw10perc: if the cumulative sales of one product id >= 10% of estimated lifetime value, the column takes value of 1, otherwise 0. However 1 can occur only once and after that the again always zero. Basically it's just an indicator of the date and transaction when a product sales exceeds the critical value of 10% of the estimated lifetime value.
The threshold value is common for all product id's (I'll just replicate the process with different thresholds at a later stage to determine which is the optimal threshold to predict future revenue).
The code I've written so far is trying to establish the cum_rev and dw1k_thresh columns, but unfortunately it doesn't work.
df_final["dw1k_thresh"] = 0
df_final["cum_rev"]= 0
opp_list =set()
for row in df_final["product id"].iteritems():
opp_list.add(row)
opp_list=list(opp_list)
opp_list=pd.Series(opp_list)
for i in opp_list:
if i == df_final["product id"].any():
df_final.cum_rev = df_final.revenue.cumsum()
for x in df_final.cum_rev:
if x >= 1000 & df_final.dw1k_thresh.sum() == 0:
df_final.dw1k_thresh = 1
else:
df_final.dw1k_thresh = 0
df_final.head(30)
