Search code examples
pandasloopsliterate-programming

iterating pandas rows using .apply()


I wanted to iterate through the pandas data frame but for some reason it does not work with .apply() method.


train = pd.read_csv('../kaggletrain')

pclass = train['Pclass']

#  pclass has list of data with either 1, 2 or 3.. 
#  so wanted to return if the cell is 1 then return True or everything False

def abc(pclass):
    if pclass == 1:
        return True 
    else: 
        return False 
    

ABCDEFG = train.apply(abc, axis=1)

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

Thank you for your help


Solution

  • ABCDEFG = train[train['pclass']==1]