For me to analyze when all 5 values are greater than zero, I use it like this:
import pandas as pd
df = pd.read_csv(csv_file)
print(df.loc[
(df['pressao1'] > 0) &
(df['pressao2'] > 0) &
(df['pressao3'] > 0) &
(df['pressao4'] > 0) &
(df['pressao5'] > 0),
'PROFIT/LOSS'
].sum())
But what if I want to calculate values when not all values are greater than zero and not all values are less than zero? Only when mixed values exist.
Example:
1,2,3,4,5 → SKIP
-1,2,3,7,-1 → SUM
-1,-2,-3,-4,-5 → SKIP
I tried using not all and not all
but it was not accepted:
print(df.loc[
not all(value>0 for value in [df['pressao1'],df['pressao2'],df['pressao3'],df['pressao4'],df['pressao5']])
and
not all(value<0 for value in [df['pressao1'],df['pressao2'],df['pressao3'],df['pressao4'],df['pressao5']]),
'PROFIT/LOSS'
].sum())
But it gives me the error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
CodePudding user response:
You can use (so we only run the filter once):
cols = df.filter(regex=r'^pressao[1-5]$')
Then use that to index into your DF:
wanted = df.loc[~cols.eq(0).any(1) & cols.lt(0).any(1) & cols.gt(0).any(1), 'PROFIT/LOSS'].sum()