How to use if statements with pandas/ csv files


February 2019


17 time


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:


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:

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.

1 answers


Ты можешь использовать np.where


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 )


   Survived     sex
0         1  female
1         0    male
2         0    male
3         1  female
4         1  female