Reputation: 393
i want to check if the string data in a series is equal to a given string. but this returns: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
i know that to use and/or i use & / | but i don't understand how to do this with an if statement
for i in range(len(data)):
if (data.Sex == 'female'):
if data.Survived == 1:
EDIT
so what i want to do is check if the Sex column is set female for each row in the data set. what is the best way of doing this. here is an example of what the data looks like:
PassengerId,Survived,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S
2,1,1,"Cumings, Mrs. John Bradley (Florence Briggs
i might be doing this all wrong. please tell me if i am.
Upvotes: 1
Views: 1854
Reputation: 82765
You can use np.where
Ex:
import pandas as pd
import numpy as np
df = pd.DataFrame({"sex": ['female', 'male', 'male' , 'female', 'female'],
"Survived": [0, 0, 0, 0, 0]})
df["Survived"] = np.where(df["sex"] == 'female', 1, 0 )
print(df)
Output:
Survived sex
0 1 female
1 0 male
2 0 male
3 1 female
4 1 female
Upvotes: 1