Pandas 查找呼叫中心经理的区域

Pandas to find a call centre manager's region

我有一种情况需要确定客户关系经理是在处理区域客户还是全球客户。现在,经理可以处理区域和全球电话,因此无论是否属实,都需要进行身份验证。数据格式如下:

Customer_Name       Customer_Region       Manager_Name          Manager_Region
A                   Middle East           MGR1                  Middle East
B                   Asia                  MGR1                  Middle East
C                   Eastern Europe        MGR1                  Middle East
D                   Middle East           MGR1                  Middle East
E                   Middle East           MGR2                  Middle East
F                   Middle East           MGR2                  Middle East
G                   Middle East           MGR3                  Eastern Europe
H                   Middle East           MGR3                  Eastern Europe

因此,如果经理能够接听他们所在地区以外的电话,那么标志应该是全球其他区域。这是一个示例输出:

Manager_Name          Manager_Call_handling_Region
MGR1                  Global
MGR2                  Regional
MGR3                  Global

你可以这样做:

df = df.groupby(by=['Manager_Name'], as_index=False).agg(','.join)
df['equal'] = df['Customer_Region']==df['Manager_Region']
df['Manager_Call_handling_Region'] = df['equal'].apply(lambda x: 'Regional' if x==True else 'Global')
df.drop(columns=['Customer_Name', 'Customer_Region', 'Manager_Region', 'equal'], inplace=True)
print(df)

输出:

  Manager_Name Manager_Call_handling_Region
0         MGR1                       Global
1         MGR2                     Regional
2         MGR3                       Global

一种方法可以是:

  1. 测试经理的区域是否与客户的区域匹配
  2. 按manager分组,求test的最小值
  3. 如果最小值为 0,则经理管理与客户不同的区域,将 0/1 转换为 Global/Regional
df["Same_Region"] = df['Customer_Region']==df['Manager_Region']
df.groupby(["Manager_Name"])["Same_Region"].min()
df["Region"] = df["Same_Region"].map({0:"Global", 1:"Regional"})

res = (df
       .set_index('Manager_Name')
        #customer column not useful
       .filter(like='Region')
       #get columns into one
       .stack()
       .groupby('Manager_Name')
        #get unique values 
       .agg(set)
       .reset_index(name='val')
        #if manager is regional, he/she will have just one location
        #if global, more than one
       .assign(Manager_Call_Handling_Region = lambda x: np.where(x.val.str.len().gt(1),
                                                                "Global",
                                                                "Regional"))
       .drop('val',axis=1)
      )

res


  Manager_Name  Manager_Call_Handling_Region
0   MGR1            Global
1   MGR2            Regional
2   MGR3            Global