0

This is my DataFrame:
DataFrame

data.where(data["Gender"] == "Male") and data.where(data["Age"] == 19)

I'm trying to print matching values but i get this error. Explain the output.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in 
----> 1 data.where(data["Gender"] == "Male") and data.where(data["Age"] == 19)

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1553             "The truth value of a {0} is ambiguous. "
   1554             "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
-> 1555                 self.__class__.__name__
   1556             )
   1557         )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Jakob F
  • 1,046
  • 10
  • 23
Saicharan Pogul
  • 89
  • 2
  • 11
  • 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) – AMC Feb 28 '20 at 20:09
  • Please do not share information as images unless absolutely necessary. Where is the [mcve]? – AMC Feb 28 '20 at 20:09

3 Answers3

2

You're getting this error as you're comparing with and when you should be using &. You should also separate with brackets. Try the following.

data[(data['Gender'] == 'Male') & (data['Age'] == 19)]

Have a look at this question for more details.

Example

For some dummy data

import numpy as np

np.random.seed(0)

df = pd.DataFrame(data = {'Gender' : np.random.choice(['Male', 'Female'], 20),
                          'Age' : np.random.randint(30, size=20)})

using the code above outputs

    Gender  Age
12    Male   19
14    Male   19

If you want to return all values, including null values, use:

data.where((data["Gender"] == "Male") & (data["Age"] == 19))
Josmoor98
  • 1,721
  • 10
  • 27
  • consider i didn't have Purchased column and I'm trying to add it by matching those independent attributes using if condition. Now i tried and again I'm getting the same error. ``` data["Purchased"] = if data[(data['Gender'] == 'Male') & (data['Age'] == 19) & (data['EstimatedSalary'] == 19000)]: Purchased = 0 data['Purchased'] = Purchased ``` – Saicharan Pogul Feb 28 '20 at 19:26
  • Try `data["Purchased"] = (data['Gender'] == 'Male') & (data['Age'] == 19) & (data['EstimatedSalary'] == 19000)` This will return a boolean, which can be converted to integers afterwards. If you want `True = 1` and `False = 0`, you can use `astype(int)` after this expression. So `data["Purchased"] = (data['Gender'] == 'Male') & (data['Age'] == 19) & (data['EstimatedSalary'] == 19000).astype(int)` – Josmoor98 Feb 28 '20 at 19:30
  • I meant `data["Purchased"] = ((data['Gender'] == 'Male') & (data['Age'] == 19) & (data['EstimatedSalary'] == 19000)).astype(int)`. This includes the extra closing brackets – Josmoor98 Feb 28 '20 at 19:37
  • @saicharanpogul Read the Pandas docs. – AMC Feb 28 '20 at 20:10
0

You need to check both conditions at once:

data.where(data["Gender"] == "Male" and data["Age"] == 19)
Jakob F
  • 1,046
  • 10
  • 23
0

Look at the operands for your and:

data.where(data["Gender"] == "Male")
    and
data.where(data["Age"] == 19)

where returns a new data frame. You're trying to perform a logical and between two data frames. What is this supposed to mean ??? :-)

As others have already pointed out, what you want is to filter rows that satisfy both conditions. Thus, you need to apply and to the conditions, not to the resulting filtered data frames:

data.where(
    data["Gender"] == "Male"
        &
    data["Age"] == 19
)

This properly performs and on a pair of Boolean values.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • For me, this still raises the same error. However, using `data.where((data["Gender"] == "Male") & (data["Age"] == 19))` doesn't. At least it does in the example code I made in my response (which i think is representative of the actual data) – Josmoor98 Feb 28 '20 at 18:56
  • 1
    Right; I forgot to edit the operator, as you pointed out earlier. – Prune Feb 28 '20 at 19:01
  • data.where((data["Gender"] == "Male") & (data["Age"] == 19)) this gives with nan values. – Saicharan Pogul Feb 28 '20 at 19:03
  • It also needs brackets around each condition, otherwise it raises a further TypeError – Josmoor98 Feb 28 '20 at 19:03
  • @Josmoor98 data.where((data["Gender"] == "Male") & (data["Age"] == 19)) and data[(data['Gender'] == 'Male') & (data['Age'] == 19)] what difference does the brackets and parentheses have? – Saicharan Pogul Feb 28 '20 at 19:07