0

I try to make a function to detect if a column has certain value but it returns ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). and Even if i check this it returns false, 'yes' in peep['anxiety'] Out[58]: False i thought it checks if there's value 'yes' in the column

if (peep['anxiety']=='yes'):
    print('correct')
else :
    print('incorrect')
random student
  • 683
  • 1
  • 15
  • 33
  • 1
    Please add that this question is about pandas. – Chris Fowl May 03 '20 at 14:15
  • 1
    You're asking if a column equals a single value which is why it throws an error. – smartse May 03 '20 at 14:16
  • @smartse oh okay, so what should it be then? – random student May 03 '20 at 14:19
  • if value in list(series): works, but there's probably a better way... – smartse May 03 '20 at 14:20
  • @potatostudent do you want to check whether 'anxiety' column has a value 'yes' or not ? – Akash Karnatak May 03 '20 at 14:21
  • 1
    @potatostudent You need to explain what you are trying to achieve. It is not clear from this question because you have not provided the full context. You might have an X/Y problem here, where you're asking about how to fix your solution, instead of how to solve your original problem. What is the problem this code is trying to solve? What is `peep` referring to here? Can you create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Mihai Chelaru May 03 '20 at 14:21
  • if ```peep['angxiety']``` is a list, why dont you do if (```'yes' in peep['angxiety']```)? – Hossein May 03 '20 at 14:21
  • @Rika it is not a list, it is a `Series`. Doing `in` will check for the **indexes**, not **values** – Tomerikoo May 03 '20 at 14:23
  • @AkashKarnatak yes i do – random student May 03 '20 at 14:24
  • you can use `peep['new_column'] = np.where(peep['anxiety']=='yes','Correct','Incorrect')` – anky May 03 '20 at 14:33
  • @MihaiChelaru i'm trying to detect in my dataframe (peep) if in its column (anxiety) it has value 'yes' i just made that very simple code that i thought would work but did not work.. – random student May 03 '20 at 14:35
  • @anky the new_column values are all incorrect – random student May 03 '20 at 14:37
  • even when `peep['anxiety']=='yes'` is True? can you check if it is an issue with case `yes v/s Yes` or leading or trailing space. Ideally you should take the approach the link to the duplicate has – anky May 03 '20 at 14:39
  • @anky yes, even when `peep['anxiety']=='yes'` is True somehow it returns all False. yeah i think i will just try another way like in that link to the duplicate – random student May 03 '20 at 14:42
  • Try `"yes" in peep["anxiety"]` inside the `np.where`. There might be extra spaces or something like that so it always fails – Tomerikoo May 03 '20 at 14:46

2 Answers2

1

If you want to check whether 'yes' is in peep['anxiety'] column then you can use unique() method of pd.Series. It returns a list of unique values of the specified Series. Now you can check whether 'yes' is there in this list using in operator,

if 'yes' in peep['anxiety'].unique():
    print('correct')
else:
    print('incorrect')

Edit:

@potatostudent's recent comment

i want to check if the column in series contains 'yes' values which if the if condition is fulfilled i want to to replace that 'yes' value with 'has anxiety' value

Then you should try this

peep[peep['anxiety'] == 'yes'] = 'has anxiety'
Akash Karnatak
  • 678
  • 2
  • 7
  • 16
1

This works:

if 'yes' in peep['anxiety'].values:
    print('correct')
else :
    print('incorrect')
smartse
  • 1,026
  • 7
  • 12