-2

Condition worked independently, but adding as part of an if, is getting truth ambiguous:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(10,5), columns=list('ABCDE'))
df1
# df1.loc[(df1.D > 1.25) | (df1.D < -0.25)] # work
# df1.loc[:,'D'] > 0.1 # work
if df1.loc[:,'D'] > 0.1:
    print(df1['A'] * df1['B'])

reference: Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

anno
  • 27
  • 7
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – TheTridentGuy supports Ukraine Jun 04 '23 at 16:03
  • that is what I put as reference too but it does not answer the question – anno Jun 04 '23 at 16:25

1 Answers1

1

df.loc[:,'D'] > 0.1 gives you a vector of true and false values:

Out[1]: 
0    False
1    False
2    False
3    False
4     True
5     True
6    False
7    False
8    False
9    False
Name: D, dtype: bool

It's not sure how to convert a vector of true and false into a single value. You could assume all: if (df1.loc[:,'D'] > 0.1).all():, any: if (df1.loc[:,'D'] > 0.1).any():, or iterate through:


for item in (df1.loc[:,'D'] > 0.1):
    if item:
        print('something')

Without knowing more about what you're trying to do python and I are just guessing.

So looking at your edit, I think you're trying to multiply A and B and take the values when D > 0.1, which is easy:

(df1['A'] * df1['B'])[(df1.loc[:,'D'] > 0.1)]

1   -0.590544
3   -0.371079
8   -0.704530
dtype: float64
Carbon
  • 3,828
  • 3
  • 24
  • 51
  • Thanks for the quick response. I have updated the question. overall I wanted to do multiplication of resulting df's two columns – anno Jun 04 '23 at 16:20
  • Think I've got what you're trying to do now – Carbon Jun 04 '23 at 16:27
  • this thing work independent but not with if..if (df1.loc[:,'D'] > 0.1): df2 = df1['A'] * df['B'] print(df2) – anno Jun 04 '23 at 16:34
  • do you want it to print if _all_ of them are > 0.1, if _any_ of them are > 0.1, or only the ones where D > 0.1? – Carbon Jun 04 '23 at 16:36
  • only the ones which are D > 0.1 – anno Jun 04 '23 at 16:37
  • you can do `print((df1['A'] * df1['B'])[(df1.loc[:,'D'] > 0.1)])`; if you want to use an if statement iterate through: `[print(df1.loc[i, 'A'] * df1.loc[i, 'B']) for i, item in enumerate(df1.loc[:, 'D']) if item > 0.1]` – Carbon Jun 04 '23 at 16:43