コード例 #1
0
def main():
    customer_tb, hotel_tb, reserve_tb = load_hotel_reserve()
    print(customer_tb)
    print(hotel_tb)
    print(reserve_tb)

    # extract columns ################################
    print('**************** specify by strings not index ****************')
    extract_01 = reserve_tb[['reserve_id', 'hotel_id', 'customer_id',
                             'reserve_datetime', 'checkin_date', 'checkin_time',
                             'checkout_date']]
    print('extract_01', extract_01)

    # extract columns using iloc ####################
    print('**************** using iloc ****************')
    extract_02 = reserve_tb.loc[:, ['reserve_id', 'hotel_id', 'customer_id',
                                     'reserve_datetime', 'checkin_date',
                                     'checkin_time', 'checkout_date']]
    print('extract_02', extract_02)

    # drop columns using drop #######################
    print('**************** using drop ****************')
    extract_03 = reserve_tb.drop(['people_num', 'total_price'], axis=1)
    print('extract_03', extract_03)

    # drop return None ##############################
    print('************* using drop inplace=True, return None *************')
    extract_04 = reserve_tb.drop(['people_num', 'total_price'], axis=1, inplace=True)
    print('extract_04', extract_04)

    # extract using query ###########################
    print('**************** using query ****************')
    extract_05 = reserve_tb.query('"2016-10-13" <= checkout_date <= "2016-10-14"')
    print('extract_05', extract_05)

    # extract random 50% sampling ####################
    print('**************** random 50% sampling ****************')
    extract_06 = reserve_tb.sample(frac=0.5)
    print('extract_06', extract_06)

    # extract group by ID sampling ##################
    print('**************** group by ID sampling ****************')
    target = pd.Series(reserve_tb['customer_id'].unique()).sample(frac=0.5)
    extract_07 = reserve_tb[reserve_tb['customer_id'].isin(target)]
    print('extract_07', extract_07)
コード例 #2
0
import pandas as pd
import numpy as np
from preprocess.load_data.data_loader import load_hotel_reserve
customer_tb, hotel_tb, reserve_tb = load_hotel_reserve()

# 下の行から本書スタート
customer_tb['sex_and_age'] = pd.Categorical(
    # 連結する列を抽出
    customer_tb[['sex', 'age']]

    # lambda関数内でsexと10代区切りのageを_を挟んで文字列として連結
    .apply(lambda x: '{}_{}'.format(x[0],
                                    np.floor(x[1] / 10) * 10), axis=1))
コード例 #3
0
from preprocess.load_data.data_loader import load_hotel_reserve
import pandas as pd
customer_tb, hotel_tb, reserve_tb = load_hotel_reserve()

# 下の行から本書スタート
# groupby関数でreserve_idを集約単位に指定し、size関数でデータ数をカウント
# groupby関数の集約処理によって行番号(index)がとびとびになっているので、
# reset_index関数によって、集約単位に指定したhotel_idを集約した状態から列名に戻し、
# 新たな行名を現在の行番号を直す
rsv_cnt_tb = reserve_tb.groupby('hotel_id').size().reset_index()

# 集約結果の列名を設定
rsv_cnt_tb.columns = ['hotel_id', 'rsv_cnt']

# groupbyでhotel_idを集約単位に指定し、
# customer_idの値をnunique関数することで顧客数をカウント
cus_cnt_tb = \
  reserve_tb.groupby('hotel_id')['customer_id'].nunique().reset_index()

# 集約結果の列名を設定
cus_cnt_tb.columns = ['hotel_id', 'cus_cnt']

# merge関数を用いて、hotel_idを結合キーとして結合(「第4章 結合」で解説)
pd.merge(rsv_cnt_tb, cus_cnt_tb, on='hotel_id')