String 使用中其他两列的“基于条件”创建列

String 使用中其他两列的“基于条件”创建列,string,pandas,for-loop,if-statement,String,Pandas,For Loop,If Statement,我想根据其他两列上的条件在pandas中创建一列。我尝试使用if-else条件的for循环,但在检查字符串值时出错 我的数据帧: 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]}) 我想根据以下条件创建列评级: 如果国家位于亚洲且销售额>2,则为1 如果国家/地

我想根据其他两列上的条件在pandas中创建一列。我尝试使用if-else条件的for循环,但在检查字符串值时出错

我的数据帧:

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]})
我想根据以下条件创建列评级:

如果国家位于亚洲且销售额>2,则为1

如果国家/地区位于NA且销售额>3,则为1

如果国家单位为欧元且销售额>=4,则为1 其他0

我正在使用一个函数:

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) 
这会引发以下错误:

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

请告诉我我在函数中做错了什么,或者其他更简单的方法。

使用矢量化解决方案:

您的解决方案应在和和中更改为isin和:

def label_race(row):
 if row['Area'] in (ASIA) and row['Sales'] >2  :
   return 1
 if row['Area'] in (NA) and row['Sales'] >3  :
   return 1  
 if row['Area'] in (EUR) and row['Sales'] >=4  :
   return 1
 return 0  

df['Rating']=df.apply(lambda row: label_race(row),axis=1) 
print (df)
      Area  Sales  Rating
0      USA      2       0
1    India      3       1
2    China      7       1
3       UK      1       0
4   France      4       1
5  Germany      3       0
6      USA      5       1
7      USA      6       1
8    India      9       1
9  Germany     10       1

不同之处在于性能:

#[10000 rows x 3 columns]
df = pd.concat([df] * 1000, ignore_index=True)

In [216]: %timeit df['Rating1']=df.apply(lambda row: label_race(row),axis=1)
275 ms ± 11.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [217]: %timeit df['Rating'] = np.select(m, [1,1,1], default=0)
215 µs ± 3.46 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
我尝试从评论中查看@Anton vBR创意:

def label_race(row):
 if row['Area'] in (ASIA) and row['Sales'] >2  :
   return 1
 elif row['Area'] in (NA) and row['Sales'] >3  :
   return 1  
 elif row['Area'] in (EUR) and row['Sales'] >=4  :
   return 1
 else:
   return 0  

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

In [223]: %timeit df['Rating1']=df.apply(lambda row: label_race(row),axis=1)
268 ms ± 2.43 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
def label_race(row):
 if row['Area'] in (ASIA) and row['Sales'] >2  :
   return 1
 elif row['Area'] in (NA) and row['Sales'] >3  :
   return 1  
 elif row['Area'] in (EUR) and row['Sales'] >=4  :
   return 1
 else:
   return 0  

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

In [223]: %timeit df['Rating1']=df.apply(lambda row: label_race(row),axis=1)
268 ms ± 2.43 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)