<jupyter_start><jupyter_text>前処理 - import - ファイル呼び出し- ファイルの中身確認<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas_profiling as pdp
FILE_PATH = '/Users/atsushisato/Google_drive/05_PBL/second_section/practice_4/'

coupon = pd.read_csv(FILE_PATH + 'coupon.csv')
customer =  pd.read_csv(FILE_PATH + 'customer.csv')
history =  pd.read_csv(FILE_PATH + 'purchase_history.csv')
#データの整形

#customer ===================================================
#カラム名変更
rename_dict = {'顧客ID':'uuid', '年齢層':'age_range', '既婚・独身':'marry', '家族人数':'fammily_num',
               '子供人数':'child_num', '持家':'house_flg', '年収レベル':'income'}
customer = customer.rename(columns=rename_dict)

#欠損値補完
customer['marry'] = customer['marry'].fillna('無回答')
customer['child_num'] = customer['child_num'].fillna('0')
customer['child_num'] = customer['child_num'].apply(lambda x: int(x.replace("+", "")))
customer['fammily_num'] = customer['fammily_num'].apply(lambda x: int(x.replace("+", "")))

#history ===================================================
#カラム名変更
history = history.rename(columns={'顧客ID':'uuid'})

#coupon ===================================================
#カラム名変更
rename_dict = {'クーポンID':'coupon_id','商品カテゴリ':'category', '商品カテゴリID':'category_id'}
coupon = coupon.rename(columns=rename_dict)


#カテゴリ別のカラム（購入回数・購入商品数・クーポン割引）に再作成
tmp_list = []

for category_id in coupon['category_id'].unique():
    column_list = ['uuid', '来店頻度', '経過日数', '総購入金額']
    column_list.extend([column for column in history.columns if category_id + '_' in column ])
    
    tmp_df = history[column_list]
    rename_dict = {category_id+'_購入回数':'purchase_num', category_id+'_購入商品数':'purchase_product_num',
                   category_id+'_クーポン割引':'coupon_use', category_id+'_購入金額':'purchase_price',
                   '来店頻度':'visits_frequency', '経過日数':'pass_days', '総購入金額':'all_purchase_price'}
    tmp_df = tmp_df.rename(columns=rename_dict)
    
    tmp_df['coupon_id'] = int(category_id.replace('c',''))
    tmp_list.append(tmp_df)    
history = pd.concat(tmp_list)

customer_df = pd.merge(customer, history, on='uuid', how='inner')
customer_df = pd.merge(customer_df, coupon, on='coupon_id', how='inner')

customer_df = customer_df.sort_values(['uuid','coupon_id']).reset_index(drop=True)
customer_df.head()
customer_df.isnull().sum()
customer_df.to_csv(FILE_PATH + 'customer_df.csv',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2257001647446458, 'fraction_numerical': 0.0016474464579901153, 'mean_word_length': 6.912052117263844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '359358', 'license': 'MIT', 'n_tokens_mistral': 1005, 'n_tokens_neox': 997, 'n_words': 120, 'path': 'notebooks/data_fomating.ipynb', 'repo_name': 'rynpssss/ai_quest_season2', 'size': '2428'}