0

I am trying to find whenever 2 columns contain the number 2 at the same time, and add it to a count.

I tried following the recommendations of this post Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() in my code:

count = 0
if (df['Bedroom2'] == 2).bool() & (df['Car'] == 2).bool():
count += 1

print(count)

However, it still displays the error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

2 Answers2

0

This is how I solved it according to the post you have linked:

import pandas as pd

data = {'col1': [1, 2, 3, 2], 'col2': [4, 2, 3, 2]}
df = pd.DataFrame(data)

count = len(df[(df['col1']==2) & (df['col2']==2)])
print(count) #outputs 2

Basically you make a dataframe that meets your conditions and count the length of it.

DGMexe
  • 43
  • 6
0

If you want to count the number of the rows where two columns contain specific numbers, you can use

count = ((df['Bedroom2'] == 2) & (df['Car'] == 2)).sum()

Your problem is that you use Series.bool() on a Series which contains more than 1 element

>>> pd.Series([True]).bool()
True

>>> pd.Series([True, False]).bool()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/.local/lib/python3.10/site-packages/pandas/core/generic.py", line 1577, in bool
    self.__nonzero__()
  File "~/.local/lib/python3.10/site-packages/pandas/core/generic.py", line 1527, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52