If we want to know if the cumulative sums are profitable in the ['Col 1','Col 2','Col 3']
columns for the long term, we do it this way:
import pandas as pd
import io
ex_csv = """
Col 1,Col 2,Col 3,return
a,b,c,1
d,e,f,1
a,e,c,-1
a,e,c,-1
d,b,c,-1
a,b,c,1
d,e,f,1
"""
df = pd.read_csv(io.StringIO(ex_csv), sep=",")
df['invest'] = df.groupby(['Col 1','Col 2','Col 3'])['return'].cumsum().gt(df['return'])
true_backs = df[(df['invest'] == True)]['return']
print(true_backs.sum())
But what if I want it to be TRUE
for the cumulative as well when not only the combination of the 3 columns is positive, but if one or two are positive too?
Example:
Perhaps the value a
of Col 1
the cumulative sum of it will be positive, but together with the values of Col 2
and Col 3
they will no longer be profitable, so in my current code it would appear as FALSE.
And I want it to be TRUE.