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
CodePudding user response:
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)
CodePudding user response:
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")
CodePudding user response:
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')