'How to sum rows from a CSV only when not all values are above zero and not all below zero?
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().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
