lundi 28 mai 2018

Create a column using based on conditions on other two columns in pandas

I want to create a column in pandas based on the conditions on other two columns. I was trying this with a for loop with if else condition but getting error in checking for string values.

My dataframe:

df=pd.DataFrame({"Area:['USA','India','China','UK','France','Germany','USA','USA','India','Germany'],
         "Sales":[2,3,7,1,4,3,5,6,9,10]})

I want to create a column RATING based on the condition:

If country is in ASIA and Sales >2, then 1

If country is in NA and Sales >3, then 1

If country is in EUR and Sales >=4, then 1 else 0

I am using a function:

ASIA=['India','China']
NA= ['USA']   
EUR=['UK','France','Germany']     
def label_race(row):
 if row['Area'].isin(ASIA) & row['Sales'] >2  :
   return 1
 if row['Area'].isin(NA) & row['Sales'] >3  :
   return 1  
 if row['Area'].isin(EUR) & row['Sales'] >=4  :
   return 1
 return 0  

df['Rating']=df.apply(lambda row: label_race(row),axis=1) 

which is throwing following error:

AttributeError: ("'str' object has no attribute 'isin'", 'occurred at index 0')

Please tell me what am I doing wrong in the function or any other way easier way to do this.

Aucun commentaire:

Enregistrer un commentaire