alex42t/CreditScore
0
1import pandas as pd2 3CAT_COLUMNS = ['currency', 'operation_kind', 'card_type',4 'operation_type', 'operation_type_group', 'ecommerce_flag',5 'payment_system', 'income_flag', 'mcc', 'country', 'city',6 'mcc_category', 'day_of_week', 'hour','weekofyear']7 8NUMERIC_COLUMNS = ['days_before', 'hour_diff']9 10REAL_COLUMNS = ['amnt']11 12 13def __amnt_pivot_table_by_column_as_frame(frame, column, agg_funcs=None) -> pd.DataFrame:14 """15 Generates pivot table for `app_id` and a specified column by aggregating `amnt` column16 17 :param frame: pd.DataFrame containing card transactions18 :param column: column with keys to group by on the pivot table column19 :param agg_funcs: list of aggregation functions, default is ['sum', 'mean', 'count']20 :return: pd.DataFrame pivot table21 """22 if agg_funcs is None:23 agg_funcs = ['sum', 'mean', 'count']24 aggs = pd.pivot_table(frame, values='amnt',25 index=['app_id'], columns=[column],26 aggfunc={'amnt': agg_funcs},27 fill_value=0)28 aggs.columns = [f'amnt_{col[0]}_{column}_{col[1]}' for col in aggs.columns.values]29 return aggs30 31 32def extract_basic_aggregations(transactions_frame: pd.DataFrame, cat_columns=None, agg_funcs=None) -> pd.DataFrame:33 """34 Extracts basic features from a card transaction dataframe35 36 :param transactions_frame: pd.DataFrame containing card transactions37 :param cat_columns: list of categorical columns for which we want to aggregate `amnt`, default is all38 :param agg_funcs: list of aggregation functions for cat_columns, default is39 ['sum', 'mean', 'count']40 :return: pd.DataFrame with extracted features41 """42 if not cat_columns:43 cat_columns = CAT_COLUMNS44 45 if not agg_funcs:46 agg_funcs = ['sum', 'mean', 'count']47 48 pivot_tables = []49 for col in cat_columns:50 pivot_tables.append(__amnt_pivot_table_by_column_as_frame(transactions_frame, column=col,51 agg_funcs=agg_funcs))52 pivot_tables = pd.concat(pivot_tables, axis=1)53 54 # we will also generate total statistics grouped by app_id55 aggs = {56 # transation amount57 'amnt': ['max', 'min', 'mean', 'median', 'sum', 'std'],58 # time difference between transactions59 'hour_diff': ['max', 'mean', 'median', 'var', 'std'],60 # days left before application at the moment when transaction took place61 'days_before': ['min', 'max', 'median']}62 63 numeric_stats = transactions_frame.groupby(['app_id']).agg(aggs)64 numeric_stats.columns = numeric_stats.columns.map('_'.join)65 66 return pd.concat([pivot_tables, numeric_stats], axis=1).reset_index()67 