2017-07-25 68 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() 

嘗試了這一點使用交叉連接,而不是一種循環您使用一次。