Python中的计算赋值函数缓慢

时间:2019-02-20 15:36:54

标签: python pandas

我正在尝试使用查找表和多个维度变量(州,国家/地区等)基于多个条件将帐户分配给销售代表

查找表在每个输入(国家,州,州,分区,垂直,焦点)上都有一列,并在应分配人员上有一列。分配表中大约有2400行。

我正在使用df.apply和该行上的lambda函数将该函数传递给数据的每一行。

我在下面粘贴的代码可以工作,但是要花30个多小时才能计算出30万行。我主要是在寻求使计算更加有效的总体方向。我觉得我应该尝试向量化查询,但是:

  1. 我不确定该怎么做。
  2. 我不确定函数中是否可以使用一次if语句。

代码如下:

# Assigns accounts to reps based on their current status, country, state, division,
# & vertical. Slightly different rules used for Acute Enterprise, as they don't
# have AM's and Sales broken out
def assign_accounts (cust_type, record_type, top_vert_arr, country, state, division, vertical, top_parent, opp_owner):

    if top_parent == 'INACTIVE - DO NOT CALL':
        return 'Joe Schmoe'

    # Check on whether the customer has joined in the last 6 months
    elif opp_owner == opp_owner:
        return opp_owner


    elif (vertical == 'Acute') & (division == 'Enterprise'):
        try:
            rep_name = assign.loc[(assign['country'] == country) &
                                (assign['state'] == state) &
                                (assign['division'] == division) &
                                (assign['vertical'] == vertical) &
                                (assign['product'] == 'Learning')]
            return rep_name['rep'].iloc[0][:]

        except:
            return 'No Map1'

    # All non-customers going to New Sales reps
    elif (top_vert_arr == 0) :
            try:
                rep_name = assign.loc[(assign['country']== country) &
                            (assign['state'] == state) &
                            (assign['division'] == division) &
                            (assign['vertical'] == vertical) &
                            (assign['focus'] == 'New')]
                return rep_name['rep'].iloc[0][:] # returns name as text
            except:
                return 'No Map5'        

    # All customers going to Account Manager assignment 
    else:
        try:
            rep_name = assign.loc[(assign['country']== country) &
                            (assign['state'] == state) &
                            (assign['division'] == division) &
                            (assign['vertical'] == vertical) &
                            (assign['focus'] == 'AM')]
            return rep_name['rep'].iloc[0][:] # returns name as text
        except:
            return 'No Map3'


#New rep assignments added to data
accounts['new_assignment'] = accounts.apply(lambda row : assign_accounts(row['type'], row['record_type'], row['top_vert_arr'], row['country'], row['bill_state'], row['division'],
                            row['short_final_vertical'], row['top_parent'], row['opp_owner']), axis = 1)

在这里提供任何定向帮助将非常感激。

0 个答案:

没有答案