A Rob4
A Rob4

Reputation: 1478

Trouble evaluating response to .loc in pandas

I have code as shown below. What I want to do is to look at the values in cells which I've accessed using .loc and test these for three conditions using an If statement.

The message that I get back is

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

I think that pandas returns a series even if there is only one item which is why this happens. But when I tried using any() I got the message

AttributeError: 'str' object has no attribute 'any'

So I'm stuck. Any help would be very much appreciated.

for my_index, row in PPI_data.iterrows():
        if PPI_data.loc[my_index,"Incident counter"]<3 and PPI_data.loc[my_index,"Risk Level"]=="Standard" and PPI_data.loc[my_index,"Crime number"]=='Not crime':
            PPI_data.set_value(my_index, 'Eligability to visit', "Eligable")
        else:
            PPI_data.set_value(my_index, 'Eligability to visit', "Not eligable")

Upvotes: 1

Views: 151

Answers (1)

jezrael
jezrael

Reputation: 862781

I think you can use rather numpy.where with mask as iterrows:

mask = (PPI_data["Incident counter"]<3) & 
       (PPI_data["Risk Level"]=="Standard") & 
       (PPI_data["Crime number"]=='Not crime')

PPI_data['Eligability to visit'] = np.where(mask,'Eligable', 'Not eligable')

Sample:

import pandas as pd

PPI_data = pd.DataFrame({'Incident counter':[5,2,1],
                   'Risk Level':["Standard","Standard","Standard"],
                   'Crime number':['Not crime',"Not crime","Crime"]})

print (PPI_data)
  Crime number  Incident counter Risk Level
0    Not crime                 5   Standard
1    Not crime                 2   Standard
2        Crime                 1   Standard

mask =  (PPI_data["Incident counter"] < 3) & 
        (PPI_data["Risk Level"]=="Standard") & 
        (PPI_data["Crime number"]=='Not crime')

print (mask)
0    False
1     True
2    False
dtype: bool

PPI_data['Eligability to visit'] = np.where(mask,'Eligable', 'Not eligable')
print (PPI_data)
  Crime number  Incident counter Risk Level Eligability to visit
0    Not crime                 5   Standard         Not eligable
1    Not crime                 2   Standard             Eligable
2        Crime                 1   Standard         Not eligable

Upvotes: 1

Related Questions