-1

Hi I imported a dataset called train and selected "Sex" from this list.

But I couldn't use the if statement on it. I get the following error :

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

I have already tried changing it to string datatype but without result.

What can i do to fix this? Changing it to a DF doesnt help same error.

import numpy as np



Sex = train['Sex']


for x in Sex : 
    if Sex == "male" :
        SurvBi = 0
    else :
        SurBi = 1
furas
  • 134,197
  • 12
  • 106
  • 148
SKbeats
  • 25
  • 4
  • 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) – fsimonjetz Sep 14 '22 at 11:57
  • 1
    it should be rather `x == "male"`. – furas Sep 14 '22 at 12:03
  • what do you want to check - `male` in every row separatelly, or globally `male` for column? First may need to keep all results in list, second doesn't need `for`-loop. – furas Sep 14 '22 at 12:04

3 Answers3

0

this solution may help

import pandas as pd

train = {
  "Sex": ["male","female"],
}
Sex = train['Sex']
for x in Sex:

    SurvBi = 0 if x == "male" else 1
    print(SurvBi)
Wael Jlassi
  • 133
  • 1
  • 9
0

Note to always check your formatting before posting your question so people have more time to solve your problem than to reconstruct your problem.

Assuming you know how to create a pandas DataFrame out of your data, this approach will work.

for index, row in df.iterrows():

    if row == "male":
        SurvBi = 0
    elif row == "female":
        SurBi = 1
    else: 
        print("invalid")
Broxy
  • 90
  • 7
0

if you want to check value for every row separatelly then you should use x == 'male'

for x in train.Sex: 
    if x == "male":
        SurBi = 0
    else:
        SurBi = 1
    print(x, SurBi)

or you can use fact that int(True) gives 1, and int(False) gives 0

for x in train.Sex:
    SurBi = int(x != "male")
    print(x, SurBi)

But if you want to use all results later then you should keep them on list

all_results = [] 

for x in train.Sex: 
    if x == "male":
        SurBi = 0
    else:
        SurBi = 1
    print(x, SurBi)
    all_results.append( SurBi )

but it would be simpler to do

all_results = (train.Sex != "male").astype(int).to_list()

But if you want to check for all column at once then you have to remember that panda (and numpy) works on vector/matrix and comparition gives also vector/matrix like [True, False, ...] but if needs single result True or False - and you have to use any( Sex == "male" ) or all( Sex == "male" ) to get single value

if any( Sex == "male" ):
   print('At least one person is male')

if all( Sex == "male" ):
   print('All persons are male')
furas
  • 134,197
  • 12
  • 106
  • 148