2017-07-25 70 views
1

我有一个产品合同的数据框(与Product_ID)。这些合约在特定日期(StartDate)打开并在特定时间关闭(CloseDate)。也有可能此时此合约处于活动状态,因此没有CloseDate。python优化熊猫群

有多个客户有合同,引用ID。这些客户在特定时刻填写调查表,这一时间以日期(Key_Date)表示。

我想要计算的是几个特征,但是在这个例子中我将关注独特产品的数量。在填写调查问卷时,我想知道某位客户有多少独特产品。

我们有一个数据框df_result,它具有客户的ID和他们在调查中填写的日期。在这个数据帧,我们也将追加计算出的特征:

import pandas as pd 
import numpy as np 
np.random.seed(256) 
df_result = pd.DataFrame({'ID' : np.random.randint(3, size=(10)), 
         'Key_Date' : pd.date_range(start=pd.datetime(2015, 5, 21), periods=10, freq='m')}) 
df_result.head() 

    ID Key_Date 
0 0 2015-05-31 
1 2 2015-06-30 
2 1 2015-07-31 
3 0 2015-08-31 
4 1 2015-09-30 

我们也有不同的合同/产品数据帧,命名为df_products

np.random.seed(321) 
df_products = pd.DataFrame({'ID' : np.random.randint(5, size=(10)), 
         'Product_ID' : np.random.randint(low = 101, high = 104, size=10), 
         'StartDate' : pd.date_range(start=pd.datetime(2015, 3, 1), periods=10, freq='m'), 
         'CloseDate' : pd.date_range(start=pd.datetime(2016, 1, 1), periods=10, freq='m')}) 
df_products.head() 

    CloseDate StartDate ID Product_ID 
0 2016-01-31 2015-03-31 4 102 
1 2016-02-29 2015-04-30 2 101 
2 2016-03-31 2015-05-31 4 102 
3 2016-04-30 2015-06-30 1 102 
4 2016-05-31 2015-07-31 0 103 

我做了一个函数来计算的独特产品填写调查的客户(合同在填写时仍处于活动状态)key_date(所以合同的开始日期(StartDate)在此日期之前,且结束日期(CloseDate)在此日期之后) 。我也希望能够在填写日期前给出一个范围,例如,过去一年中所有独特的产品都是活跃的。所以即使是11个月前的封闭合约也会包括在内。我通过给出一个额外的参数timeperiod来做到这一点,我减去填写日期(创建一个新的日期:low_date)。然后,CloseDate必须晚于low_date,而不是key_date

def unique_products(df,timeperiod,ID,key_date): 
    low_date = key_date - relativedelta(months=timeperiod) 
    data = df.loc[(df['StartDate'] <= key_date) & 
        (df['CloseDate'] >= low_date) & 
       (df['ID'] == ID)].groupby(['ID'], as_index = False)['Product_ID'].nunique().reset_index() 
    if 'Product_ID' in list(data): 
     try: 
      return float(data['Product_ID']) 
     except: 
      return np.nan 

此后,我在df_result追加在名为unique_products新列这些值:

df_result['unique_products'] = df_result.apply(lambda row: unique_products(df_products, 3, row['ID'], row['Key_Date']), axis=1) 
df_result.head() 


    ID Key_Date unique_products 
0 0 2015-05-31 NaN 
1 2 2015-06-30 1.0 
2 1 2015-07-31 1.0 
3 0 2015-08-31 1.0 
4 1 2015-09-30 2.0 

但是将其应用到我的整个dateset时,它变得非常缓慢,由于这样的事实,每个surveyrow必须因为他们有不同的时间被评估。有什么办法可以改善吗?

感谢任何输入:)

回答

1

您需要使用合并。

merged = pd.merged(df_products,df_results,how='left',on='ID') 

现在合并将有df_products的所有列有“关键日期”一起,如果它为null,则该人未填写了调查。

filled_survey = merged.loc[~(merged['Key Date'].isnull())] 

现在您可以通过减去相关日期并相应地过滤来找到timedelta。

0
df_result['low_date'] = df_result['key_date'] - relativedelta(months=timeperiod) #creating low_date column 
df_result2 = pandas.merge(df_result,df_products,how = "outer",on = "ID") #Join both the tables 
df_result2 = df_result2[(df_result2['StartDate'] <= df_result2['key_date']) & (df_result2['CloseDate'] >= df_result2['low_date'])] # Filter conditions 
df_result2 = df_result2.groupby(['ID','Key_Date'])['Product_ID'].nunique().reset_index() 

尝试了这一点使用交叉连接,而不是一种循环您使用一次。