๋ฐ์ดํฐ ์ฌ์ด์ธ์ค 100๋ฒ์ ๋ ธํฌ(๊ตฌ์กฐํ ๋ฐ์ดํฐ ์ฒ๋ฆฌํธ) โ Python Part 3 (Q41 to Q60)์ ํด์ค์ ๋๋ค.
ย
ย
์ฒ์์
- ๋จผ์ ๋ค์ ์ ์ ์คํํฉ๋๋ค.
- ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ ธ์ค๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ต๋๋ค(PostgreSQL).
- ์ฌ์ฉํ ๊ฒ์ผ๋ก ์์๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ค์ ์ ์์ ๊ฐ์ ธ์ต๋๋ค.
- ์ฌ์ฉํ๋ ค๋ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ ๊ฒฝ์ฐ install.packages()๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ ํ๊ฒ ์ค์นํฉ๋๋ค.
- ์ด๋ฆ, ์ฃผ์ ๋ฑ์ ๋๋ฏธ ๋ฐ์ดํฐ์ด๋ฉฐ ์ค์ ๋ฐ์ดํฐ๊ฐ ์๋๋๋ค.
ย
import os
import pandas as pd
import numpy as np
from datetime import datetime, date
from dateutil.relativedelta import relativedelta
import math
import psycopg2
from sqlalchemy import create_engine
from sklearn import preprocessing
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.model_selection import TimeSeriesSplit
from imblearn.under_sampling import RandomUnderSampler
if 'PG_PORT' in os.environ:
pgconfig = {
'host': 'db',
'port': os.environ['PG_PORT'],
'database': os.environ['PG_DATABASE'],
'user': os.environ['PG_USER'],
'password': os.environ['PG_PASSWORD'],
}
# pd.read_sql์ฉ ์ปค๋ฅํฐ
conn = psycopg2.connect(**pgconfig)
df_customer = pd.read_sql(sql='select * from customer', con=conn)
df_category = pd.read_sql(sql='select * from category', con=conn)
df_product = pd.read_sql(sql='select * from product', con=conn)
df_receipt = pd.read_sql(sql='select * from receipt', con=conn)
df_store = pd.read_sql(sql='select * from store', con=conn)
df_geocode = pd.read_sql(sql='select * from geocode', con=conn)
else:
if not os.path.exists('../data/'):
!git clone https://github.com/The-Japan-DataScientist-Society/100knocks-preprocess
os.chdir('100knocks-preprocess/docker/work/answer')
dtype = {
'customer_id': str,
'gender_cd': str,
'postal_cd': str,
'application_store_cd': str,
'status_cd': str,
'category_major_cd': str,
'category_medium_cd': str,
'category_small_cd': str,
'product_cd': str,
'store_cd': str,
'prefecture_cd': str,
'tel_no': str,
'postal_cd': str,
'street': str
}
df_customer = pd.read_csv("../data/customer.csv", dtype=dtype)
df_category = pd.read_csv("../data/category.csv", dtype=dtype)
df_product = pd.read_csv("../data/product.csv", dtype=dtype)
df_receipt = pd.read_csv("../data/receipt.csv", dtype=dtype)
df_store = pd.read_csv("../data/store.csv", dtype=dtype)
df_geocode = pd.read_csv("../data/geocode.csv", dtype=dtype)
์ฐ์ต๋ฌธ์
P-041: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก(amount)์ ๋ ์ง(sales_ymd)๋ณ๋ก ์ง๊ณํ์ฌ, ์ง๋๋ฒ ๋งค์ถ์ด ์์๋ ๋ ๋ก๋ถํฐ์ ๋งค์ถ ๊ธ์ก ์ฆ๊ฐ์ ๊ณ์ฐํ๋ผ. ๊ทธ๋ฆฌ๊ณ ๊ฒฐ๊ณผ๋ฅผ 10๊ฑด ํ์ํ๋ผ.
df_sales_amount_by_date = df_receipt[['sales_ymd', 'amount']].\
groupby('sales_ymd').sum().reset_index()
df_sales_amount_by_date = pd.concat([df_sales_amount_by_date,
df_sales_amount_by_date.shift()], axis=1)
df_sales_amount_by_date.columns = ['sales_ymd','amount','lag_ymd','lag_amount']
df_sales_amount_by_date['diff_amount'] = \
df_sales_amount_by_date['amount'] - df_sales_amount_by_date['lag_amount']
df_sales_amount_by_date.head(10)
ย | sales_ymd | amount | lag_ymd | lag_amount | diff_amount |
---|---|---|---|---|---|
0 | 20170101 | 33723 | NaN | NaN | NaN |
1 | 20170102 | 24165 | 20170101.0 | 33723.0 | -9558.0 |
2 | 20170103 | 27503 | 20170102.0 | 24165.0 | 3338.0 |
3 | 20170104 | 36165 | 20170103.0 | 27503.0 | 8662.0 |
4 | 20170105 | 37830 | 20170104.0 | 36165.0 | 1665.0 |
5 | 20170106 | 32387 | 20170105.0 | 37830.0 | -5443.0 |
6 | 20170107 | 23415 | 20170106.0 | 32387.0 | -8972.0 |
7 | 20170108 | 24737 | 20170107.0 | 23415.0 | 1322.0 |
8 | 20170109 | 26718 | 20170108.0 | 24737.0 | 1981.0 |
9 | 20170110 | 20143 | 20170109.0 | 26718.0 | -6575.0 |
ํด์ค:
์ด ์ฝ๋๋ ํ ๊ธฐ์ ์ ์ผ์ผ ๋งค์ถ์ก์ ๊ณ์ฐํ๊ณ , ๋งค์ถ์ก์ ํ๋ฃจ์ฉ ๋ค๋ก ๋ฏธ๋ค ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์์ ๋ง๋ค๊ณ , ์ ๋ ๋งค์ถ์ก์ ๋ํ๋ด๋ ์๋ก์ด ์ด์ ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ๋ฃจ์ ๋งค์ถ ๊ธ์ก๊ณผ ์ ๋ ์ ๋งค์ถ ๊ธ์ก์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํ์ฌ 'diff_amount'๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์์ฑํฉ๋๋ค.
์๋์์ ํ ์ค์ฉ ์ค๋ช ํ๊ฒ ์ต๋๋ค.
df_sales_amount_by_date = df_receipt[['sales_ymd', 'amount']].groupby('sales_ymd').sum().reset_index(): ์ด ์ฝ๋๋ 'df_receipt' ๋ฐ์ดํฐ ํ๋ ์์์ ' sales_ymd'์ 'amount' ์ด์ ์ ํํ์ฌ 'sales_ymd' ์ด์ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋ฃนํํ๊ณ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ 'sales_ymd' ๊ทธ๋ฃน์ 'amount' ์ปฌ๋ผ์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ , ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ์ฌ 'sales_ymd'์ 'amount' ์ปฌ๋ผ์ ๊ฐ์ง ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์ 'df_sales_amount_by_date'๋ฅผ ์์ฑํ๋ค.
df_sales_amount_by_date = pd.concat([df_sales_amount_by_date, df_sales_amount_by_date.shift()], axis=1): ์ด ์ฝ๋๋ 'df_sales_amount_by_date' ๋ฐ์ดํฐํ๋ ์์ 'df_sales_amount_by_date' ๋ฐ์ดํฐํ๋ ์์ date' dataframe์ ์ด์ ์ถ์ ๋ฐ๋ผ(์ฆ, ์ํ์ผ๋ก) ์ด๋ํ ๋ฒ์ ๊ณผ ์ฐ๊ฒฐํ๋ค. ๊ทธ ๊ฒฐ๊ณผ 4๊ฐ์ ์ปฌ๋ผ์ ๊ฐ์ง ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์์ด ๋ง๋ค์ด์ง๋ค. sales_ymd', 'amount', 'lag_ymd', 'lag_amount' 4๊ฐ์ ์ด์ ๊ฐ์ง ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์์ด ๋๋ค.
df_sales_amount_by_date.columns = ['sales_ymd','amount','lag_ymd','lag_amount']: ์ด ์ฝ๋๋ 'df_sales_amount_by_date' ๋ฐ์ดํฐํ๋ ์์ ์ปฌ๋ผ ์ด๋ฆ์ ๋ ์๋ฏธ ์๋ ์ด๋ฆ์ผ๋ก ๋ณ๊ฒฝํ๋ค.
df_sales_amount_by_date['diff_amount'] = df_sales_amount_by_date['amount'] - df_sales_amount_by_date['lag_amount']: 'df_sales_amount_by_date' dataframe์ amount_by_date' dataframe์ 'amount' ์ปฌ๋ผ์์ 'lag_amount' ์ปฌ๋ผ์ ๋นผ์ ์๋ก์ด ์ปฌ๋ผ 'diff_amount'๋ฅผ ์์ฑํ๋ค.
df_sales_amount_by_date.head(10). ์ด ์ฝ๋๋ 'df_sales_amount_by_date' dataframe์ ์ฒ์ 10์ค์ ํ์ํ๋ค.
ย
P-042: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก(amount)์ ๋ ์ง(sales_ymd)๋ณ๋ก ์ง๊ณํ๊ณ , ๊ฐ ๋ ์ง์ ๋ฐ์ดํฐ์ ๋ํด ์ด์ , ์ ์ , 3ํ ์ ์ ๋งค์ถ์ด ์์๋ ๋ ์ ๋ฐ์ดํฐ๋ฅผ ๊ฒฐํฉํ๋ผ. ๊ทธ๋ฆฌ๊ณ ๊ฒฐ๊ณผ๋ฅผ 10๊ฑด ํ์ํ๋ผ.
# ์ฝ๋ ์์ 1: ์ธ๋กํ ์ผ์ด์ค
df_sales_amount_by_date = df_receipt[['sales_ymd', 'amount']]. \
groupby('sales_ymd').sum().reset_index()
for i in range(1, 4):
df_tmp = pd.concat([df_sales_amount_by_date,
df_sales_amount_by_date.shift(i)], axis=1)
if i == 1:
df_lag = df_tmp
else:
df_lag = df_lag.append(df_tmp)
df_lag.columns = ['sales_ymd', 'amount', 'lag_ymd', 'lag_amount']
df_lag.dropna().astype(int).sort_values(['sales_ymd','lag_ymd']).head(10)
ย | sales_ymd | amount | lag_ymd | lag_amount |
---|---|---|---|---|
1 | 20170102 | 24165 | 20170101 | 33723 |
2 | 20170103 | 27503 | 20170101 | 33723 |
2 | 20170103 | 27503 | 20170102 | 24165 |
3 | 20170104 | 36165 | 20170101 | 33723 |
3 | 20170104 | 36165 | 20170102 | 24165 |
3 | 20170104 | 36165 | 20170103 | 27503 |
4 | 20170105 | 37830 | 20170102 | 24165 |
4 | 20170105 | 37830 | 20170103 | 27503 |
4 | 20170105 | 37830 | 20170104 | 36165 |
5 | 20170106 | 32387 | 20170103 | 27503 |
ํด์ค:
์ด ์ฝ๋๋ ๋งค์ถ ๋ฐ์ดํฐ์ ๋ํด ์ง์ฐ ๋ถ์์ ์คํํ๊ณ ์์ต๋๋ค.
๋จผ์ ์ด ์ฝ๋๋ ๋งค์ถ ๋ฐ์ดํฐ๋ฅผ ๋ ์ง๋ณ๋ก ๊ทธ๋ฃนํํ์ฌ ๊ฐ ๋ ์ง์ ๋งค์ถ ๊ธ์ก์ ํฉ์ฐํ์ฌ ๊ฐ ๋ ์ง์ ๋งค์ถ ๊ธ์ก์ ํฉ์ ๊ตฌํฉ๋๋ค. ์ด๋ df_sales_amount_by_date์ ์ ์ฅ๋ฉ๋๋ค.
๊ทธ๋ฐ ๋ค์ 1~3๊น์ง 3ํ ๋ฐ๋ณตํ๋ for ๋ฃจํ๋ฅผ ์คํํฉ๋๋ค.
for ๋ฃจํ์ ๊ฐ ๋ฐ๋ณต์์ df_sales_amount_by_date์ DataFrame์ iํ์ฉ ์ด๊ธ๋๊ฒ ์ฐ๊ฒฐํ์ฌ df_tmp๋ผ๋ ์๋ก์ด DataFrame์ ์์ฑํ๋ค. ๊ทธ ๊ฒฐ๊ณผ ๊ฐ ํ์ด ํน์ ๋ ์ง์ ๋งค์ถ ๋ฐ์ดํฐ์ ํ ๋ ์ง ์ด์ ๋ ์ง์ ๋งค์ถ ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๋ DataFrame์ด ๋๋ค.
i๊ฐ 1์ด๋ฉด ๊ฒฐ๊ณผ DataFrame์ df_lag์ ์ ์ฅ๋๋ค. ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, DataFrame์ df_lag์ ์ถ๊ฐ๋๋ค.
df_lag์ ์ด์ ํ์ฌ ๋ ์ง์ ์ง์ฐ๋ ๋ ์ง์ ๋งค์ถ์ ๋ํ๋ด๊ธฐ ์ํด sales_ymd, amount, lag_ymd, lag_amount๋ก ์ด๋ฆ์ด ๋ณ๊ฒฝ๋๋ค.
๋ง์ง๋ง์ผ๋ก NaN ๊ฐ์ด ํฌํจ๋ ํ(์ฒซ ๋ฒ์งธ iํ์์ ๋ฐ์)์ ์ญ์ ํ๊ณ ๋งค์ถ ๊ธ์ก์ ์ ์๋ก ๋ณํํ ํ sales_ymd์ lag_ymd๋ฅผ ๊ธฐ์ค์ผ๋ก DataFrame์ ์ ๋ ฌํ์ฌ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๋ค.
์ด๋ ๊ฒ ํ๋ฉด ๊ฐ ๋ ์ง์ ๋งค์ถ ๊ธ์ก ํฉ๊ณ์ 1์ผ ์ , 2์ผ ์ , 3์ผ ์ ์ ๋งค์ถ ๊ธ์ก ํฉ๊ณ๊ฐ ํ์๋์ด ์ฌ์ฉ์๋ ๋งค์ถ ๋ฐ์ดํฐ์ ๋ํ ์ง์ฐ ๋ถ์์ ํ ์ ์๋ค.
# ์ฝ๋ ์์ 2: ๊ฐ๋กํ ์ผ์ด์ค
df_sales_amount_by_date = df_receipt[['sales_ymd', 'amount']].\
groupby('sales_ymd').sum().reset_index()
df_lag = df_sales_amount_by_date
for i in range(1, 4):
df_lag = pd.concat([df_lag, df_sales_amount_by_date.shift(i)], axis=1)
columns = [f'lag_ymd_{i}', f'lag_amount_{i}']
df_lag.columns = list(df_lag.columns)[:-len(columns)] + columns
df_lag.dropna().astype(int).sort_values(['sales_ymd']).head(10)
ย | sales_ymd | amount | lag_ymd_1 | lag_amount_1 | lag_ymd_2 | lag_amount_2 | lag_ymd_3 | lag_amount_3 |
---|---|---|---|---|---|---|---|---|
3 | 20170104 | 36165 | 20170103 | 27503 | 20170102 | 24165 | 20170101 | 33723 |
4 | 20170105 | 37830 | 20170104 | 36165 | 20170103 | 27503 | 20170102 | 24165 |
5 | 20170106 | 32387 | 20170105 | 37830 | 20170104 | 36165 | 20170103 | 27503 |
6 | 20170107 | 23415 | 20170106 | 32387 | 20170105 | 37830 | 20170104 | 36165 |
7 | 20170108 | 24737 | 20170107 | 23415 | 20170106 | 32387 | 20170105 | 37830 |
8 | 20170109 | 26718 | 20170108 | 24737 | 20170107 | 23415 | 20170106 | 32387 |
9 | 20170110 | 20143 | 20170109 | 26718 | 20170108 | 24737 | 20170107 | 23415 |
10 | 20170111 | 24287 | 20170110 | 20143 | 20170109 | 26718 | 20170108 | 24737 |
11 | 20170112 | 23526 | 20170111 | 24287 | 20170110 | 20143 | 20170109 | 26718 |
12 | 20170113 | 28004 | 20170112 | 23526 | 20170111 | 24287 | 20170110 | 20143 |
ย
ํด์ค:
์ ๊ณต๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ์ฒ๋ฆฌ๋ฅผ ์ํํ๋ค.
df_receipt์ DataFrame์ ์ฌ์ฉํ์ฌ sales_ymd์ amount ์ด์ ์ ํํ๊ณ , sales_ymd๋ก ๊ทธ๋ฃนํํ๊ณ , amount ์ด์ sum() ํจ์๋ฅผ ์ ์ฉํ์ฌ ๊ฐ ๋ ์ง์ ์ด๋งค์ถ์ก์ ๊ณ์ฐํ๋ค. ์์ฑ๋ DataFrame์ df_sales_amount_by_date์ ์ ์ฅ๋๋ค.
df_sales_amount_by_date์ ๋ณต์ฌ๋ณธ์ผ๋ก df_lag๋ผ๋ ์๋ก์ด DataFrame์ด ์์ฑ๋๋ค.
๋ฃจํ๊ฐ 3ํ(1~4ํ) ์คํ๋๊ณ , ๊ฐ ๋ฐ๋ณต์์ shift() ๋ฉ์๋๊ฐ ์ฌ์ฉ๋์ด df_sales_amount_by_date DataFrame์ i์ฃผ๊ธฐ(์ผ) ๋งํผ ์์ฐจ๋ฅผ ๋๋ค. ์ป์ด์ง DataFrame์ pd.concat()์ ์ฌ์ฉํ์ฌ df_lag์ ์ํ์ผ๋ก ์ฐ๊ฒฐํ์ฌ df_lag์ ์ ์ฅํ๋ค.
df_lag DataFrame์ ์ถ๊ฐ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ด๋ฆ์ ์ ์ฅํ๊ธฐ ์ํด columns ๋ณ์๊ฐ ์์ฑ๋๋ค.
df_lag์ ์ปฌ๋ผ ์ด๋ฆ์ columns ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ์ฌ ์ ๋ฐ์ดํธ๋๊ณ , ๊ฒฐ๊ณผ DataFrame์ df_lag์ ์ ์ฅ๋๋ค.
Dropna() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ์ธก๊ฐ์ด ์๋ ํ์ ์ญ์ ํ๊ณ , astype() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ DataFrame์ ์ ์ํ์ผ๋ก ๋ณํํ๋ค. ๋ง์ง๋ง์ผ๋ก sort_values() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ DataFrame์ sales_ymd์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ , head() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ DataFrame์ ์ฒซ 10๊ฐ์ ํ์ ๋ฐํํ๊ณ ์๋ค.
์์ฝํ๋ฉด, ์ด ์ฝ๋์์๋ ํ๋งค ์ค์ ์ ์ถ์ธ๋ฅผ ํ์ ํ๊ธฐ ์ํด ๊ฐ ๋ ์ง์ ํ๋งค ๊ธ์ก๊ณผ ์ง๋ 3์ผ๊ฐ์ ํ๋งค ๊ธ์ก์ ํฌํจํ๋ DataFrame df_lag๋ฅผ ์์ฑํ๊ณ ์์ต๋๋ค. ์์ฑ๋ DataFrame์ ๊ฐ ๋ ์ง์ ํ๋งค ๊ธ์ก๊ณผ ์ง๋ 3์ผ๊ฐ์ ํ๋งค ๊ธ์ก์ ํ๋งค์ผ ์์๋๋ก ์ ๋ ฌํ ๊ฒ์ ๋๋ค.
P-043๏ผ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)๋ฅผ ๊ฒฐํฉํ์ฌ ์ฑ๋ณ ์ฝ๋(gender_cd)์ ์ฐ๋ น(age์์ ๊ณ์ฐ)๋ณ ๋งค์ถ ๊ธ์ก(amount)์ ํฉ์ฐํ ๋งค์ถ ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ค. ์ฑ๋ณ ์ฝ๋๋ 0์ ๋จ์ฑ, 1์ ์ฌ์ฑ, 9๋ ์ ์ ์์์ ๋ํ๋ธ๋ค.
๋จ, ํญ๋ชฉ ๊ตฌ์ฑ์ ์ฐ๋ น, ์ฌ์ฑ ๋งค์ถ๊ธ์ก, ๋จ์ฑ ๋งค์ถ๊ธ์ก, ๋จ์ฑ ๋งค์ถ๊ธ์ก, ์ฑ๋ณ ๋ฏธ์ ๋งค์ถ๊ธ์ก์ 4๊ฐ ํญ๋ชฉ์ผ๋ก ๊ตฌ์ฑํ๋ค(์ธ๋ก๋ก ์ฐ๋ น, ๊ฐ๋ก๋ก ์ฑ๋ณ ๊ต์ฐจ ์ง๊ณ). ๋ํ ์ฐ๋ น์ 10์ธ ๋จ์์ ๊ณ๊ธ์ผ๋ก ํ๋ค.
# ์ฝ๋ ์์ 1
df_tmp = pd.merge(df_receipt, df_customer, how ='inner', on="customer_id")
df_tmp['era'] = df_tmp['age'].apply(lambda x: math.floor(x / 10) * 10)
df_sales_summary = pd.pivot_table(
df_tmp, index='era',
columns='gender_cd',
values='amount',
aggfunc='sum'
).reset_index()
df_sales_summary.columns = ['era', 'male', 'female', 'unknown']
df_sales_summary
ย | era | male | female | unknown |
---|---|---|---|---|
0 | 10 | 1591.0 | 149836.0 | 4317.0 |
1 | 20 | 72940.0 | 1363724.0 | 44328.0 |
2 | 30 | 177322.0 | 693047.0 | 50441.0 |
3 | 40 | 19355.0 | 9320791.0 | 483512.0 |
4 | 50 | 54320.0 | 6685192.0 | 342923.0 |
5 | 60 | 272469.0 | 987741.0 | 71418.0 |
6 | 70 | 13435.0 | 29764.0 | 2427.0 |
7 | 80 | 46360.0 | 262923.0 | 5111.0 |
8 | 90 | NaN | 6260.0 | NaN |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์์ ์ํํฉ๋๋ค.
๋ ๊ฐ์ ๋ฐ์ดํฐ ํ๋ ์ df_receipt์ df_customer๋ฅผ ๊ณตํต ์ด customer_id๋ก ๋ด๋ถ ๊ฒฐํฉํ์ฌ ๋ณํฉํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์ df_tmp์ ์ ์ฅํฉ๋๋ค.
df_tmp ๋ฐ์ดํฐ ํ๋ ์์ ์๋ก์ด ์ด era๋ฅผ ์์ฑํฉ๋๋ค. era๋ ๊ฐ ๊ณ ๊ฐ์ ๋์ด๋ฅผ 10์ผ๋ก ๋๋๊ณ 10 ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผํ์ฌ ๊ณ์ฐํฉ๋๋ค.
era์ gender_cd ๊ฐ์ ๊ณ ์ ํ ์กฐํฉ๋ง๋ค df_tmp ๋ฐ์ดํฐ ํ๋ ์์ amount ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ pd.pivot_table() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์ df_sales_summary๋ก ์ ์ฅํฉ๋๋ค. ๊ฒฐ๊ณผ ๋ฐ์ดํฐ ํ๋ ์์ era ๊ฐ์ ์ธ๋ฑ์ค๋ก, gender_cd ๊ฐ์ ์ด๋ก ๊ฐ์ง๊ณ ์๋ค.
df_sales_summary ๋ฐ์ดํฐ ํ๋ ์์ ์ด ์ด๋ฆ์ ์ข ๋ ์ดํดํ๊ธฐ ์ฌ์ด ์ด๋ฆ์ผ๋ก ๋ณ๊ฒฝํ๋ค. ์ฒซ ๋ฒ์งธ ์ด์ era์ด๊ณ , ๊ทธ ๋ค์ male, female, unknown์ด ์ด์ด์ง๋ฉฐ ๊ฐ gender_cd ๊ฐ์ ๊ธ์ก ์ด์ ํฉ๊ณ๋ฅผ ๋ํ๋ธ๋ค.
# ์ฝ๋ ์์ 1
df_tmp = pd.merge(df_receipt, df_customer, how ='inner', on="customer_id")
df_tmp['era'] = np.floor(df_tmp['age'] / 10).astype(int) * 10
df_sales_summary = pd.pivot_table(df_tmp, index='era', columns='gender_cd',
values='amount', aggfunc='sum').reset_index()
df_sales_summary.columns = ['era', 'male', 'female', 'unknown']
df_sales_summary
ย | era | male | female | unknown |
---|---|---|---|---|
0 | 10 | 1591.0 | 149836.0 | 4317.0 |
1 | 20 | 72940.0 | 1363724.0 | 44328.0 |
2 | 30 | 177322.0 | 693047.0 | 50441.0 |
3 | 40 | 19355.0 | 9320791.0 | 483512.0 |
4 | 50 | 54320.0 | 6685192.0 | 342923.0 |
5 | 60 | 272469.0 | 987741.0 | 71418.0 |
6 | 70 | 13435.0 | 29764.0 | 2427.0 |
7 | 80 | 46360.0 | 262923.0 | 5111.0 |
8 | 90 | NaN | 6260.0 | NaN |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํฉ๋๋ค.
df_receipt์ df_customer ๋ ๊ฐ์ ๋ฐ์ดํฐ ํ๋ ์์ ๋ด๋ถ ๊ฒฐํฉ์ ์ฌ์ฉํ์ฌ ๊ณตํต ์ด 'customer_id'์์ ๊ฒฐํฉํฉ๋๋ค. ๊ฒฐ๊ณผ ๋ฐ์ดํฐ ํ๋ ์์ df_tmp๋ผ๋ ์๋ก์ด ๋ณ์์ ์ ์ฅ๋๋ค.
'age' ์ด์ 10์ผ๋ก ๋๋์ด ์ธต์๋ฅผ ๊ตฌํ๊ณ 10์ ๊ณฑํ์ฌ 10๋ ์ ๊ตฌํ์ฌ df_tmp์ ์๋ก์ด 'era' ์ด์ ์์ฑํ๋ค. ์ด ์ด์ ๊ฐ ๊ณ ๊ฐ์ ์ฐ๋ น๋๋ฅผ ์์ญ ๋ ๋จ์๋ก ๋ํ๋ธ๋ค.
pd.pivot_table() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ df_sales_summary๋ผ๋ ํผ๋ฒ ํ ์ด๋ธ์ ์์ฑํ๋ค. ์ด ํผ๋ฒ ํ ์ด๋ธ์ df_tmp ๋ฐ์ดํฐ ํ๋ ์์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฉฐ, 'era' ์ด๋ก ์ธ๋ฑ์ฑ๋ ํ, 'gender_cd' ์ด๋ก ๊ทธ๋ฃนํ๋ ์ด, sum ์ง๊ณ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ 'amount' ์ด์์ ๊ณ์ฐ๋ ๊ฐ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๊ฒฐ๊ณผ ํ๋ ๊ฐ ์ฐ๋ น๋์ ๊ฐ ์ฑ๋ณ์ ๋ํ ํ๋งค ๊ธ์ก์ ํฉ๊ณ๋ฅผ ๋ณด์ฌ์ค๋ค.
df_sales_summary์ ์ปฌ๋ผ ์ด๋ฆ์ ์ข ๋ ์๊ธฐ ์ฝ๊ฒ ๋ณ๊ฒฝํ์ฌ 'male', 'female', 'unknown'์ด ๊ฐ ์ฐ๋ น๋์ ๊ฐ ์ฑ๋ณ์ ์ด ๋งค์ถ์ก์ ๋ํ๋ธ๋ค.
ย
P-044๏ผ 043์์ ๋ง๋ ๋งค์ถ ์์ฝ ๋ฐ์ดํฐ(df_sales_summary)๋ ์ฑ๋ณ ๋งค์ถ์ ๊ฐ๋ก๋ก ๋์ดํ ๋ฐ์ดํฐ์๋ค. ์ด ๋ฐ์ดํฐ์์ ์ฑ๋ณ์ ์ธ๋ก๋ก ๊ฐ์ ธ์์ ์ฐ๋ น, ์ฑ๋ณ ์ฝ๋, ๋งค์ถ ๊ธ์ก์ ์ธ ๊ฐ์ง ํญ๋ชฉ์ผ๋ก ๋ณํํ๋ผ. ๋จ, ์ฑ๋ณ ์ฝ๋๋ ๋จ์ฑ์ โ00โ, ์ฌ์ฑ์ โ01โ, ์ ์ ์์์ โ99โ๋ก ํ๋ค.
df_sales_summary.set_index('era'). \
stack().reset_index().replace({'female':'01','male':'00','unknown':'99'}). \
rename(columns={'level_1':'gender_cd', 0: 'amount'})
ย | era | gender_cd | amount |
---|---|---|---|
0 | 10 | 00 | 1591.0 |
1 | 10 | 01 | 149836.0 |
2 | 10 | 99 | 4317.0 |
3 | 20 | 00 | 72940.0 |
4 | 20 | 01 | 1363724.0 |
5 | 20 | 99 | 44328.0 |
6 | 30 | 00 | 177322.0 |
7 | 30 | 01 | 693047.0 |
8 | 30 | 99 | 50441.0 |
9 | 40 | 00 | 19355.0 |
10 | 40 | 01 | 9320791.0 |
11 | 40 | 99 | 483512.0 |
12 | 50 | 00 | 54320.0 |
13 | 50 | 01 | 6685192.0 |
14 | 50 | 99 | 342923.0 |
15 | 60 | 00 | 272469.0 |
16 | 60 | 01 | 987741.0 |
17 | 60 | 99 | 71418.0 |
18 | 70 | 00 | 13435.0 |
19 | 70 | 01 | 29764.0 |
20 | 70 | 99 | 2427.0 |
21 | 80 | 00 | 46360.0 |
22 | 80 | 01 | 262923.0 |
23 | 80 | 99 | 5111.0 |
24 | 90 | 01 | 6260.0 |
ํด์ค:
์ด ์ฝ๋๋ pandas์ DataFrame df_sales_summary๋ฅผ ์ธ๋ฑ์ค๋ฅผ era๋ก ์ค์ ํ๊ณ , ์๋ก์ด ์นผ๋ผ gender_cd์ ์ฌ์ฑ, ๋จ์ฑ, ์ ์ ์์์ ์๊ณ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ์ฌ ๋ณํํ๊ณ ์๋ค. ๊ทธ๋ฆฌ๊ณ gender_cd์ ๊ฐ์ 01(์ฌ์ฑ), 00(๋จ์ฑ), 99(์ ์ ์์)๋ก ๋์ฒดํ๊ณ ์์ต๋๋ค. ๋ง์ง๋ง์ผ๋ก level_1์ gender_cd๋ก, 0์ amount๋ก ์ปฌ๋ผ ์ด๋ฆ์ ๋ณ๊ฒฝํฉ๋๋ค.
๋ค์์ ๋จ๊ณ๋ณ ์ค๋ช ์ ๋๋ค.
df_sales_summary.set_index('era'): df_sales_summary์ ์ธ๋ฑ์ค๋ฅผ era๋ผ๋ ์ปฌ๋ผ์ผ๋ก ์ค์ ํ๋ค. ์ด๋ฅผ ํตํด era๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฒ ๊ทธ๋ฃนํํ๊ฑฐ๋ ํผ๋ฒํ ์ ์์ต๋๋ค.
.stack(): female, male, unknown์ ๊ฐ ์ปฌ๋ผ์ ํ๋์ ์ปฌ๋ผ์ผ๋ก ์คํํ๊ณ , ์คํ๋ ์ปฌ๋ผ์ ์๋ก์ด ์ธ๋ฑ์ค ๋ ๋ฒจ์ ์ถ๊ฐํ๋ค.
.reset_index(): DataFrame์ ์ธ๋ฑ์ค๋ฅผ ๊ธฐ๋ณธ ์ ์ ์ธ๋ฑ์ค๋ก ์ฌ์ค์ ํ๋ค.
.replace({'female':'01','male':'00','unknown':'99'}): gender_cd ์ปฌ๋ผ์ ๊ฐ์ ํด๋น ์ฝ๋๋ก ๋์ฒดํ๋ค.
.rename(columns={'level_1':'gender_cd', 0: 'amount'}): level_1 ์ปฌ๋ผ์ gender_cd๋ก, 0์ amount๋ก ์ด๋ฆ์ ๋ฐ๊พผ๋ค, ์คํ๋ ์ปฌ๋ผ์ ์๋ ์ปฌ๋ผ ์ด๋ฆ์ ํฌํจํ๊ณ ์๋ค.
P-045: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ์๋ ์์ผ(birth_day)์ ๋ ์งํ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด์ ํ๊ณ ์๋ค. ์ด๋ฅผ YYYYMMDD ํ์์ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ๊ณ ๊ฐ ID(customer_id)์ ํจ๊ป 10๊ฑด ํ์ํ๋ผ.
# ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก YYYYMMDD ํ์์ ๋ฌธ์์ด๋ก ๋ณํํ ์ ์๋ค.
# pd.to_datetime(df_customer['birth_day']).dt.strftime('%Y%m%d')
pd.concat([df_customer['customer_id'],
pd.to_datetime(df_customer['birth_day']).dt.strftime('%Y%m%d')],
axis = 1).head(10)
ย | customer_id | birth_day |
---|---|---|
0 | CS021313000114 | 19810429 |
1 | CS037613000071 | 19520401 |
2 | CS031415000172 | 19761004 |
3 | CS028811000001 | 19330327 |
4 | CS001215000145 | 19950329 |
5 | CS020401000016 | 19740915 |
6 | CS015414000103 | 19770809 |
7 | CS029403000008 | 19730817 |
8 | CS015804000004 | 19310502 |
9 | CS033513000180 | 19620711 |
ํด์ค:
์ด ์ฝ๋์์๋ ๊ธฐ์กด df_customer DataFrame์ customer_id ์ปฌ๋ผ๊ณผ birth_day ์ปฌ๋ผ(๋ ์ง ์ ๋ณด) ๋ ์ปฌ๋ผ์ ์ฐ๊ฒฐํ์ฌ ์๋ก์ด DataFrame์ ์์ฑํ๊ณ ์์ต๋๋ค.
birth_day ์ด์ pd.to_datetime() ํจ์๋ฅผ ์ ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๊ณ , dt.strftime('%Y%m%d') ๋ฉ์๋๋ฅผ ์ ์ฉํ์ฌ datetime์ '%Y%m%d'(์ฆ, ๋ , ์, ์ผ) ํ์์ ๋ฌธ์์ด๋ก ํฌ๋งทํ๊ณ ์์ต๋๋ค.
์ด๋ ๊ฒ ํ๋ฉด ์๋ก์ด DataFrame์ ๊ณ ๊ฐ์ ์๋ ์์ผ์ ๋ฌธ์์ด๋ก ํํํ ์๋ก์ด ์ด์ด ์์ฑ๋ฉ๋๋ค. ๋ง์ง๋ง์ผ๋ก head() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ DataFrame์ ์ฒซ 10๊ฐ์ ํ์ ํ์ํฉ๋๋ค.
P-046: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ์ ์ฒญ์ผ(application_date)์ YYYYMMDD ํ์์ ๋ฌธ์์ด ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด์ ํ๊ณ ์๋ค. ์ด๋ฅผ ๋ ์งํ์ผ๋ก ๋ณํํ์ฌ ๊ณ ๊ฐ ID(customer_id)์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ผ.
pd.concat([df_customer['customer_id'],
pd.to_datetime(df_customer['application_date'])], axis=1).head(10)
ย | customer_id | application_date |
---|---|---|
0 | CS021313000114 | 2015-09-05 |
1 | CS037613000071 | 2015-04-14 |
2 | CS031415000172 | 2015-05-29 |
3 | CS028811000001 | 2016-01-15 |
4 | CS001215000145 | 2017-06-05 |
5 | CS020401000016 | 2015-02-25 |
6 | CS015414000103 | 2015-07-22 |
7 | CS029403000008 | 2015-05-15 |
8 | CS015804000004 | 2015-06-07 |
9 | CS033513000180 | 2015-07-28 |
ํด์ค:
์ด ์ฝ๋ ์ค๋ํซ์ pd.concat() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ pandas DataFrame df_customer์ ๋ ์ปฌ๋ผ('customer_id'์ 'application_date')์ ์๋ก์ด DataFrame์ผ๋ก ์ฐ๊ฒฐํ๋ค.
pd.to_datetime() ๋ฉ์๋๋ 'application_date' ์ปฌ๋ผ์ datetime ๊ฐ์ฒด๋ก ๋ณํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ด ๋ฉ์๋๋ ๋ค์ํ ํ์์ ๋ ์ง ๋ฌธ์์ด์ pandas์ ํ์ค datetime ๊ฐ์ฒด๋ก ๋ณํํ๋ ํธ๋ฆฌํ ๋ฐฉ๋ฒ์ด๋ค.
๊ฒฐ๊ณผ DataFrame์ 'customer_id'์ 'application_date' ๋ ๊ฐ์ ์ปฌ๋ผ์ ๊ฐ์ง๋ฉฐ, ๋ ์ง๋ datetime ํ์์ผ๋ก ํ์๋๋ค.
ย
P-047: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ์ผ(sales_ymd)์ YYYYMMDD ํ์์ ์ซ์ํ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด์ ํ๊ณ ์๋ค. ์ด๋ฅผ ๋ ์งํ์ผ๋ก ๋ณํํ์ฌ ์์์ฆ ๋ฒํธ(receipt_no), ์์์ฆ ํ์๋ฒํธ(receipt_sub_no)์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ผ.
pd.concat([df_receipt[['receipt_no', 'receipt_sub_no']],
pd.to_datetime(df_receipt['sales_ymd'].astype('str'))],
axis=1).head(10)
ย | receipt_no | receipt_sub_no | sales_ymd |
---|---|---|---|
0 | 112 | 1 | 2018-11-03 |
1 | 1132 | 2 | 2018-11-18 |
2 | 1102 | 1 | 2017-07-12 |
3 | 1132 | 1 | 2019-02-05 |
4 | 1102 | 2 | 2018-08-21 |
5 | 1112 | 1 | 2019-06-05 |
6 | 1102 | 2 | 2018-12-05 |
7 | 1102 | 1 | 2019-09-22 |
8 | 1112 | 2 | 2017-05-04 |
9 | 1102 | 1 | 2019-10-10 |
ํด์ค:
์ด ์ฝ๋๋ pd.concat ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๊ฐ์ pandas ๋ฐ์ดํฐ ํ๋ ์์ ๊ฒฐํฉํ๋ค.
df_receipt[['receipt_no', 'receipt_sub_no']]: df_receipt ๋ฐ์ดํฐ ํ๋ ์์์ receive_no์ receive_sub_no ์ปฌ๋ผ์ ์ ํํ๊ณ ์์ต๋๋ค.
pd.to_datetime(df_receipt['sales_ymd'].astype('str')): df_receipt dataframe์ sales_ymd ์ปฌ๋ผ์ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
pd.concat์ axis=1์ด๋ผ๋ ์ธ์๋ ๋ฐ์ดํฐํ๋ ์์ ์ํ์ผ๋ก ์ด ๋ฐฉํฅ์ผ๋ก ์ฐ๊ฒฐํ๋ค๋ ๊ฒ์ ๋ํ๋ธ๋ค. ์์ฑ๋ dataframe์ receive_no, receive_sub_no, sales_ymd(datetime ํ์)์ ์ด์ ๊ฐ์ง๋ฉฐ, ์ฒ์ 10๊ฐ์ ํ์ด .head(10) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ์๋๋ค.
P-048: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ์ํฌํฌ ์ด(sales_epoch)๋ ์ซ์ํ UNIX ์ด๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด์ ํ๊ณ ์๋ค. ์ด๋ฅผ ๋ ์งํ์ผ๋ก ๋ณํํ์ฌ ์์์ฆ ๋ฒํธ(receipt_no), ์์์ฆ ์๋ธ๋ฒํธ(receipt_sub_no)์ ํจ๊ป 10๊ฑด์ ํ์ํ๋ผ.
pd.concat([df_receipt[['receipt_no', 'receipt_sub_no']],
pd.to_datetime(df_receipt['sales_epoch'], unit='s').rename('sales_ymd')],
axis=1).head(10)
ย | receipt_no | receipt_sub_no | sales_ymd |
---|---|---|---|
0 | 112 | 1 | 2018-11-03 |
1 | 1132 | 2 | 2018-11-18 |
2 | 1102 | 1 | 2017-07-12 |
3 | 1132 | 1 | 2019-02-05 |
4 | 1102 | 2 | 2018-08-21 |
5 | 1112 | 1 | 2019-06-05 |
6 | 1102 | 2 | 2018-12-05 |
7 | 1102 | 1 | 2019-09-22 |
8 | 1112 | 2 | 2017-05-04 |
9 | 1102 | 1 | 2019-10-10 |
ํด์ค:
์ด ์ฝ๋์์๋ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํฉ๋๋ค.
df_receipt DataFrame์์ receipt_no์ receipt_sub_no ๋ ๊ฐ์ ์ปฌ๋ผ์ ์ ํํฉ๋๋ค.
pandas ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ sales_epoch ์ปฌ๋ผ์ datetime ํ์์ผ๋ก ๋ณํํ๊ณ , unit ํ๋ผ๋ฏธํฐ์๋ ์ ๋ ฅ์ด ์ด ๋จ์์์ ๋ํ๋ด๋ 's'๋ฅผ ์ค์ ํ๋ค.
์์ฑ๋ datetime ์ปฌ๋ผ์ pandas ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ rename() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ 'sales_ymd'๋ก ์ด๋ฆ์ ๋ฐ๊พผ๋ค.
๋ง์ง๋ง์ผ๋ก pandas ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ concat() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๊ฐ์ DataFrame์ ์ด์ ์ถ(axis=1)์ ๋ฐ๋ผ ์ฐ๊ฒฐํ๋ค.
์์ฑ๋ DataFrame์ receive_no, receive_sub_no, sales_ymd์ ์ธ ๊ฐ์ ์ปฌ๋ผ์ ๊ฐ์ง๋ฉฐ, sales_ymd๋ datetime ํ์์ ํ๋งค ๋ ์ง ๋ฐ ์๊ฐ์ผ๋ก, head(10) ํจ์๋ ๊ฒฐ๊ณผ DataFrame์ ์ฒซ ๋ฒ์งธ 10๊ฐ์ ํ์ ํ์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
ย
P-049: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ์ํฌํฌ ์ด(sales_epoch)๋ฅผ ๋ ์งํ์ผ๋ก ๋ณํํ์ฌ โ์ฐ๋โ๋ง ์ถ์ถํ์ฌ ์์์ฆ ๋ฒํธ(receipt_no), ์์์ฆ ํ์ ๋ฒํธ(receipt_sub_no)์ ํจ๊ป 10๊ฑด ํ์ํ๋ผ.
pd.concat([df_receipt[['receipt_no', 'receipt_sub_no']],
pd.to_datetime(df_receipt['sales_epoch'],
unit='s').dt.year.rename('sales_year')],
axis=1).head(10)
ย | receipt_no | receipt_sub_no | sales_year |
---|---|---|---|
0 | 112 | 1 | 2018 |
1 | 1132 | 2 | 2018 |
2 | 1102 | 1 | 2017 |
3 | 1132 | 1 | 2019 |
4 | 1102 | 2 | 2018 |
5 | 1112 | 1 | 2019 |
6 | 1102 | 2 | 2018 |
7 | 1102 | 1 | 2019 |
8 | 1112 | 2 | 2017 |
9 | 1102 | 1 | 2019 |
ํด์ค:
์ด ์ฝ๋๋ df_receipt์ receive_no์ receive_sub_no ์ด๊ณผ to_datetime ๋ฉ์๋๋ก datetime ๊ฐ์ฒด๋ก ๋ณํํ sales_epoch ์ด์ ์ฐ๋๋ฅผ ์ฐ๊ฒฐํ์ฌ ์๋ก์ด DataFrame์ ์์ฑํ๊ณ , ๋จ์ ๋งค๊ฐ๋ณ์๋ฅผ s๋ก ์ค์ ํ์ฌ ์ค์ ํ์ฌ sales_epoch์ ๊ฐ์ด ์ ๋์ค ํ์์คํฌํ(์ฆ, 1970๋ 1์ 1์ผ๋ถํฐ์ ์ด ๋จ์)์์ ๋ํ๋ ๋๋ค.
์๋ ์ฝ๋๋ฅผ ํ ์ค์ฉ ๋ถํดํด ๋ณด๊ฒ ์ต๋๋ค.
pd.concat: ๋ ๊ฐ์ DataFrame์ ์ด์ ์ถ์ ๋ฐ๋ผ ์ฐ๊ฒฐํ๋ค.
df_receipt[['receipt_no', 'receipt_sub_no']]: df_receipt DataFrame์์ receipt_no์ receipt_sub_no ์ปฌ๋ผ์ ์ ํํฉ๋๋ค.
pd.to_datetime(df_receipt['sales_epoch'], unit=s'): to_datetime ๋ฉ์๋์์ sales_epoch ์ปฌ๋ผ์ ๊ฐ์ datetime ๊ฐ์ฒด๋ก ๋ณํํ๊ณ unit ํ๋ผ๋ฏธํฐ์ s๋ฅผ ์ค์ ํ๋ค.
dt.year: datetime ๊ฐ์ฒด์ year ์ปดํฌ๋ํธ๋ฅผ ์ถ์ถํ๋ค.
rename('sales_year'): ๊ฒฐ๊ณผ ์ปฌ๋ผ ์ด๋ฆ์ sales_year๋ก ๋ณ๊ฒฝํ๋ค.
axis=1: ์ด ๋ฐฉํฅ์ผ๋ก ์ฐ๊ฒฐํ๋๋ก ์ง์ ํ๋ค.
.head(10): ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ๋ฐํํ๋ค.
P-050: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ์ํฌํฌ ์ด(sales_epoch)๋ฅผ ๋ ์งํ์ผ๋ก ๋ณํํ์ฌ โ์โ๋ง ์ถ์ถํ์ฌ ์์์ฆ ๋ฒํธ(receipt_no), ์์์ฆ ํ์ ๋ฒํธ(receipt_sub_no)์ ํจ๊ป 10๊ฑด์ ํ์ํ๋ค. ๋จ, โ์โ์ 0์ผ๋ก ์ฑ์์ง 2์๋ฆฌ๋ก ์ถ์ถํ๋ค.
# dt.month๋ก๋ ์์ ๊ตฌํ ์ ์์ง๋ง, ์ฌ๊ธฐ์๋ 0์ผ๋ก ์ฑ์์ง 2์๋ฆฌ๋ก ๊ตฌํ๊ธฐ ์ํด strftime์ ์ด์ฉํ๊ณ ์๋ค.
df_datetime = pd.to_datetime(df_receipt['sales_epoch'],
unit='s').rename('sales_month')
pd.concat([df_receipt[['receipt_no', 'receipt_sub_no']],
df_datetime.dt.strftime('%m')],axis=1).head(10)
ย | receipt_no | receipt_sub_no | sales_month |
---|---|---|---|
0 | 112 | 1 | 11 |
1 | 1132 | 2 | 11 |
2 | 1102 | 1 | 07 |
3 | 1132 | 1 | 02 |
4 | 1102 | 2 | 08 |
5 | 1112 | 1 | 06 |
6 | 1102 | 2 | 12 |
7 | 1102 | 1 | 09 |
8 | 1112 | 2 | 05 |
9 | 1102 | 1 | 10 |
ํด์ค:
์ฃผ์ด์ง ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ์ฒ๋ฆฌ๋ฅผ ์ํํ๋ค.
'df_receipt' dataframe์์ 'sales_epoch' ์ปฌ๋ผ์ ์ถ์ถํ๋ค.
'to_datetime' ํจ์๋ฅผ ์ฌ์ฉํ์ฌ 'sales_epoch' ๊ฐ์ datetime ํ์์ผ๋ก ๋ณํํ๊ณ , 'unit' ํ๋ผ๋ฏธํฐ๋ฅผ ์ด๋ฅผ ๋ํ๋ด๋ 's'๋ก ์ค์ ํ๋ค.
'rename' ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ ๊ณ์ด์ ์ด๋ฆ์ 'sales_month'๋ก ๋ณ๊ฒฝํ๋ค.
'strftime' ํจ์๋ก 'sales_month' ์ปฌ๋ผ์์ '%m' ํ์์ผ๋ก ์ ๊ฐ์ ์ถ์ถํ๋ค.
'concat' ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ ๊ณ์ด์ 'df_receipt' ๋ฐ์ดํฐํ๋ ์์ 'receipt_no' ๋ฐ 'receipt_sub_no' ์ปฌ๋ผ๊ณผ ์ฐ๊ฒฐํ๋ค.
๊ฒฐ๊ณผ ๋ฐ์ดํฐ ํ๋ ์์ 'receipt_no', 'receipt_sub_no', 'sales_month' ์ด์ ํฌํจํ๋ฉฐ, 'sales_month'์๋ 'sales_epoch' ์ด์์ ์ถ์ถํ ์ ๊ฐ์ด ๋ค์ด๊ฐ๋ค.
ย
P-051: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ์ํฌํฌ ์ด๋ฅผ ๋ ์งํ์ผ๋ก ๋ณํํ์ฌ โ์ผโ๋ง ์ถ์ถํ์ฌ ์์์ฆ ๋ฒํธ(receipt_no), ์์์ฆ ํ์ ๋ฒํธ(receipt_sub_no)์ ํจ๊ป 10๊ฑด ํ์ํ๋ค. ๋จ, โ์ผโ์ 0์ผ๋ก ์ฑ์์ง 2์๋ฆฌ๋ก ์ถ์ถํ๋ค.
# dt.day๋ก๋ ๋ ์ง๋ฅผ ๊ตฌํ ์ ์์ง๋ง, ์ฌ๊ธฐ์๋ 0์ผ๋ก ์ฑ์์ง 2์๋ฆฌ๋ก ๊ตฌํ๊ธฐ ์ํด strftime์ ์ด์ฉํ๊ณ ์๋ค.
df_datetime = pd.to_datetime(df_receipt['sales_epoch'],
unit='s').rename('sales_day')
pd.concat([df_receipt[['receipt_no', 'receipt_sub_no']],
df_datetime.dt.strftime('%d')], axis=1).head(10)
ย | receipt_no | receipt_sub_no | sales_day |
---|---|---|---|
0 | 112 | 1 | 03 |
1 | 1132 | 2 | 18 |
2 | 1102 | 1 | 12 |
3 | 1132 | 1 | 05 |
4 | 1102 | 2 | 21 |
5 | 1112 | 1 | 05 |
6 | 1102 | 2 | 05 |
7 | 1102 | 1 | 22 |
8 | 1112 | 2 | 04 |
9 | 1102 | 1 | 10 |
ํด์ค:
์ด ์ฝ๋๋ df_receipt DataFrame์์ sales_epoch ์ปฌ๋ผ์ ๊ฐ์ ธ์ 1970๋ 1์ 1์ผ(์ ๋์ค ์๊ฐ์ด๋ผ๊ณ ๋ ํจ)๋ถํฐ์ ์ด๋ฅผ ์ ์ฅํ๊ณ pd.to_datetime()์ ์ฌ์ฉํ์ฌ ์ด ๋จ์์ ์ ๋ฐ๋๋ฅผ ๊ฐ์ง Pandas datetime ๊ฐ์ฒด๋ก ๋ณํํฉ๋๋ค. ์์ฑ๋ datetime ๊ฐ์ฒด๋ sales_day๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ํ ๋น๋๋ค.
๊ทธ๋ฐ ๋ค์ strftime() ๋ฉ์๋๊ฐ sales_day ์ปฌ๋ผ์ ์ ์ฉ๋์ด 2์๋ฆฌ ์ซ์์ ์์ 0(%d๋ก ํ์๋จ)์ ํํ๋ก ๋ ์ง์ ์ผ ์ฑ๋ถ์ ์ถ์ถํ๋ค. ์ป์ด์ง ๋ฌธ์์ด์ pd.concat()์ ์ฌ์ฉํ์ฌ recipate_no ๋ฐ recipate_sub_no ์ด๊ณผ ์ฐ๊ฒฐํ์ฌ ์ธ ๊ฐ์ ์ด์ ๊ฐ์ง ์๋ก์ด DataFrame์ ์์ฑํ๋ค.
๋ง์ง๋ง์ผ๋ก ๊ฒฐ๊ณผ DataFrame์ ๋ํด head() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๋ค. ์ด ์ฝ๋์์๋ df_receipt์ sales_epoch ์ด์์ ์๊ณผ ๋ ์ง๋ฅผ ์ถ์ถํ๊ณ ์ด๋ฅผ ์์์ฆ ๋ฒํธ์ ๊ฒฐํฉํ์ฌ ์๋ก์ด DataFrame์ ์์ฑํ๊ณ ์๋ค.
ย
P-052: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก(amount)์ ๊ณ ๊ฐ ID(customer_id)๋ณ๋ก ํฉ์ฐํ ํ, ๋งค์ถ ๊ธ์ก ์ดํฉ์ ๋ํด 2,000์ ์ดํ๋ฅผ 0, 2,000์๋ณด๋ค ํฐ ๊ธ์ก์ 1๋ก ์ด๋ถํํ์ฌ ๊ณ ๊ฐ ID, ๋งค์ถ ๊ธ์ก ์ดํฉ๊ณผ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ค. ๋จ, ๊ณ ๊ฐ ID๊ฐ โZโ๋ก ์์ํ๋ ๊ฒ์ ๋นํ์์ ์๋ฏธํ๋ฏ๋ก ์ ์ธํ์ฌ ๊ณ์ฐํ๋ค.
# ์ฝ๋ ์์ 1
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")',
engine='python')
df_sales_amount = df_sales_amount[['customer_id', 'amount']]. \
groupby('customer_id').sum().reset_index()
df_sales_amount['sales_flg'] = df_sales_amount['amount']. \
apply(lambda x: 1 if x > 2000 else 0)
df_sales_amount.head(10)
ย | customer_id | amount | sales_flg |
---|---|---|---|
0 | CS001113000004 | 1298 | 0 |
1 | CS001114000005 | 626 | 0 |
2 | CS001115000010 | 3044 | 1 |
3 | CS001205000004 | 1988 | 0 |
4 | CS001205000006 | 3337 | 1 |
5 | CS001211000025 | 456 | 0 |
6 | CS001212000027 | 448 | 0 |
7 | CS001212000031 | 296 | 0 |
8 | CS001212000046 | 228 | 0 |
9 | CS001212000070 | 456 | 0 |
ํด์ค:
์ด ์ฝ๋์์๋ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํฉ๋๋ค.
df_receipt์์ query ๋ฉ์๋๋ก customer_id๊ฐ 'Z'๋ก ์์ํ์ง ์๋ ํ์ ์ ํํ์ฌ df_sales_amount์ ๋์ ํ๋ค.
df_sales_amount์์ customer_id์ amount ์ด์ ์ ํํ๊ณ groupby ๋ฉ์๋๋ก customer_id๋ณ๋ก ๊ธ์ก์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๋ค. ๊ทธ ๊ฒฐ๊ณผ๋ฅผ df_sales_amount์ ๋์ ํ๋ค.
df_sales_amount์ ๋๋ค ํจ์๋ฅผ ์ด์ฉํ apply ๋ฉ์๋๋ก ๊ธ์ก์ด 2000๋ณด๋ค ํฌ๋ฉด 1, ๊ทธ๋ ์ง ์์ผ๋ฉด 0์ ํฌํจํ๋ ์๋ก์ด ์ด sales_flg๋ฅผ ์ถ๊ฐํ๋ค.
์์ฑ๋ DataFrame df_sales_amount์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๋ค.
์ฆ, ๊ฒฐ๊ณผ DataFrame df_sales_amount์๋ customer_id, ๊ฐ ๊ณ ๊ฐ์ ์ด ์ง์ถ์ก, ๊ณ ๊ฐ์ ์ง์ถ์ด ๋ง์์ง(2000๋ณด๋ค ํฐ์ง) ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ฐ์ด๋๋ฆฌ ํ๋๊ทธ sales_flg๊ฐ ํฌํจ๋์ด ์๋ค.
# ์ฝ๋ ์์ 2๏ผnp.where์ ํ์ฉ๏ผ
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")',
engine='python')
df_sales_amount = df_sales_amount[['customer_id', 'amount']]. \
groupby('customer_id').sum().reset_index()
df_sales_amount['sales_flg'] = np.where(df_sales_amount['amount'] > 2000, 1, 0)
df_sales_amount.head(10)
ย | customer_id | amount | sales_flg |
---|---|---|---|
0 | CS001113000004 | 1298 | 0 |
1 | CS001114000005 | 626 | 0 |
2 | CS001115000010 | 3044 | 1 |
3 | CS001205000004 | 1988 | 0 |
4 | CS001205000006 | 3337 | 1 |
5 | CS001211000025 | 456 | 0 |
6 | CS001212000027 | 448 | 0 |
7 | CS001212000031 | 296 | 0 |
8 | CS001212000046 | 228 | 0 |
9 | CS001212000070 | 456 | 0 |
ํด์ค:
์ด ์ฝ๋๋ ๊ณ ๊ฐ์ ๋งค์ถ ์ ๋ณด ์์ฝ ํ ์ด๋ธ์ ์์ฑํ๊ณ ์์ต๋๋ค. ์๋๋ ๊ฐ ํ์ ์ฒ๋ฆฌ ๋ด์ฉ์ ๋๋ค.
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")', engine='python'): ์ด ํ์ ID๊ฐ "Z"๋ก ์์ํ๋ ๊ณ ๊ฐ์ ์ ์ธํ๋๋ก ์์์ฆ ๋ฐ์ดํฐ๋ฅผ ํํฐ๋งํ๊ณ ์์ต๋๋ค. ํฉ๋๋ค. ์ฌ๊ธฐ์๋ query ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํํฐ๋ฅผ ์ ์ฉํ๊ณ , engine='python'์ ์ง์ ํ์ฌ ๊ฒฝ๊ณ ๋ฉ์์ง๋ฅผ ํผํ๊ณ ์์ต๋๋ค. ํํฐ๋ง๋ ๋ฐ์ดํฐ๋ df_sales_amount๋ผ๋ ์๋ก์ด dataframe์ ํ ๋น๋ฉ๋๋ค.
df_sales_amount = df_sales_amount[['customer_id', 'amount']].groupby('customer_id').sum().reset_index(): ์ด ๋ผ์ธ์ df_sales_amount ๋ฐ์ดํฐ ํ๋ ์์ customer_id๋ก ๊ทธ๋ฃนํํ์ฌ ๊ฐ ๊ทธ๋ฃน์ amount ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ , customer_id๋ฅผ ๋ค์ ์ด๋ก ๋ง๋ค๊ธฐ ์ํด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ๊ณ ์๋ค. ๊ทธ ๊ฒฐ๊ณผ ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก์ ํฉ๊ณ๋ฅผ ๋ํ๋ด๋ ๋ฐ์ดํฐ ํ๋ ์์ ์ป์ ์ ์์ต๋๋ค.
df_sales_amount['sales_flg'] = np.where(df_sales_amount['amount'] > 2000, 1, 0): ์ด ํ์ df_sales_amount ๋ฐ์ดํฐ ํ๋ ์์ sales_flg๋ผ๋ ์๋ก์ด ์ด์ ์ถ๊ฐํ๋ค. ์ด ์ด์ ๊ฐ์ ๊ณ ๊ฐ์ ์ด ๋งค์ถ ๊ธ์ก(amount)์ด 2000๋ณด๋ค ํฐ์ง ์๋์ง์ ๋ฐ๋ผ ์ค์ ๋๋ค. ์ฌ๊ธฐ์๋ np.where ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์กฐ๊ฑด์ ์ ์ฉํ๊ณ ์ ์ด์ 1 ๋๋ 0์ ํ ๋นํ๊ณ ์์ต๋๋ค.
df_sales_amount.head(10): ์ด ํ์ df_sales_amount ๋ฐ์ดํฐ ํ๋ ์์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๋ค. ๊ทธ ๊ฒฐ๊ณผ customer_id, ๋งค์ถ ์ด์ก, ๊ณ ๊ฐ์ sales_flg๊ฐ 1(๋งค์ถ ๊ธ์ก์ด 2000๋ณด๋ค ํฐ ๊ฒฝ์ฐ) ๋๋ 0(๋งค์ถ ๊ธ์ก์ด 2000 ์ดํ์ธ ๊ฒฝ์ฐ)์ธ์ง๋ฅผ ๋ํ๋ด๋ ํ ์ด๋ธ์ด ํ์๋๋ค.
P-053: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ์ฐํธ๋ฒํธ(postal_cd)์ ๋ํด ๋์ฟ(์ 3์๋ฆฌ๊ฐ 100~209์ธ ๊ฒ)๋ฅผ 1, ๊ทธ ์ธ์ ๊ฒ์ 0์ผ๋ก ์ด์งํํ๋ผ. ๋ํ ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๊ฒฐํฉํ์ฌ ์ ์ฒด ๊ธฐ๊ฐ ๋์ ๋งค์ถ ์ค์ ์ด ์๋ ๊ณ ๊ฐ ์๋ฅผ ์์ฑํ ์ดํญ๋์๋ณ๋ก ๊ณ์ฐํ๋ผ.
# ์ฝ๋ ์์ 1
df_tmp = df_customer[['customer_id', 'postal_cd']].copy()
df_tmp['postal_flg'] = df_tmp['postal_cd']. \
apply(lambda x: 1 if 100 <= int(x[0:3]) <= 209 else 0)
pd.merge(df_tmp, df_receipt, how='inner', on='customer_id'). \
groupby('postal_flg').agg({'customer_id':'nunique'})
ย | customer_id |
---|---|
postal_flg | ย |
0 | 3906 |
1 | 4400 |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํฉ๋๋ค.
"df_customer" DataFrame์์ "customer_id"์ "postal_cd" ์ปฌ๋ผ์ ๋ณต์ฌํ์ฌ "df_tmp"๋ผ๋ ์๋ก์ด DataFrame์ ๋์ ํฉ๋๋ค.
"postal_cd" ์ปฌ๋ผ์ ์ฒซ 3์๋ฆฌ๋ฅผ ๊ธฐ์ค์ผ๋ก "df_tmp"์ ์๋ก์ด ์ปฌ๋ผ "postal_flg"๋ฅผ ์์ฑํ๋ค. ์ฒ์ 3์๋ฆฌ๊ฐ 100์์ 209 ์ฌ์ด์ด๋ฉด "postal_flg"๋ 1๋ก ์ค์ ๋๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด 0์ผ๋ก ์ค์ ๋๋ค.
"customer_id" ์ปฌ๋ผ์ "df_tmp"์ "df_receipt"๋ฅผ ๋ด๋ถ ๊ฒฐํฉ์ผ๋ก ๊ฒฐํฉํ๋ค.
๊ฒฐ๊ณผ DataFrame์ "postal_flg" ์ปฌ๋ผ์ผ๋ก ๊ทธ๋ฃนํํ๊ณ , "nunique()" ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ทธ๋ฃน ๋ด ๊ณ ์ ํ ๊ณ ๊ฐ ID์ ์๋ฅผ ๊ณ์ฐํ๋ค.
'postal_flg' ์ด๋ก ๊ทธ๋ฃนํ๋ ๊ณ ์ ๊ณ ๊ฐ ID์ ๊ฐ์๋ฅผ ๋ํ๋ด๋ ๊ฒฐ๊ณผ DataFrame์ ๋ฐํํ๋ค.
์์ฝํ๋ฉด, ์ด ์ฝ๋๋ ๊ตฌ๋งค ๋ด์ญ์ ๊ธฐ๋ฐ์ผ๋ก ํน์ ์ฐํธ๋ฒํธ ์ง์ญ์ ๊ฑฐ์ฃผํ๋ ๊ณ ๊ฐ ์๋ฅผ ๋ถ์ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
ย
# ์ฝ๋ ์์ 2๏ผnp.whereใbetween์ ํ์ฉ๏ผ
df_tmp = df_customer[['customer_id', 'postal_cd']].copy()
df_tmp['postal_flg'] = np.where(df_tmp['postal_cd'].str[0:3].astype(int)
.between(100, 209), 1, 0)
pd.merge(df_tmp, df_receipt, how='inner', on='customer_id'). \
groupby('postal_flg').agg({'customer_id':'nunique'})
ย | customer_id |
---|---|
postal_flg | ย |
0 | 3906 |
1 | 4400 |
ํด์ค:
์ด ์ฝ๋๋ ๊ณ ๊ฐ์ ์ฐํธ๋ฒํธ๊ฐ ํน์ ๋ฒ์์ ์๋์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ postal_flg ์ด์ ๊ฐ์ง ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์ df_tmp๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ๋ค์์ ์ฝ๋์ ๋ํ ๋จ๊ณ๋ณ ์ค๋ช ์ ๋๋ค.
df_tmp = df_customer[['customer_id', 'postal_cd']].copy() df_customer์์ customer_id์ postal_cd ์ด๋ง ํฌํจํ๋ ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์ df_tmp๋ฅผ ์์ฑํ๋ค.
df_tmp['postal_flg'] = np.where(df_tmp['postal_cd'].str[0:3].astype(int).between(100, 209), 1, 0) df_tmp์ np.where ํจ์๋ฅผ ์ฌ์ฉํ์ฌ postal_flg ์ด์ ์๋ก ์์ฑํ๋ค. str[0:3] ์ฝ๋์์ ์ฐํธ๋ฒํธ์ ์ฒซ 3๊ธ์๋ฅผ ๋ฌธ์์ด๋ก ๊ฐ์ ธ์ astype(int)๋ก ์ ์๋ก ๋ณํํ๊ณ , between ํจ์๋ ๊ทธ ์ ์๊ฐ 100์์ 209 ๋ฒ์์ ์๋์ง ํ์ธํฉ๋๋ค. ๋ง์ฝ ๊ทธ๋ ๋ค๋ฉด 1์ด postal_flg์ ๋์ ๋๊ณ , ๊ทธ๋ ์ง ์๋ค๋ฉด 0์ด ๋์ ๋ฉ๋๋ค.
pd.merge(df_tmp, df_receipt, how='inner', on='customer_id') df_tmp์ df_receipt๋ฅผ customer_id ์ด๋ก ๋ณํฉํ์ฌ ๊ณ ๊ฐ์ด ๋ ๋ฐ์ดํฐ ํ๋ ์์ ๋ชจ๋ ์กด์ฌํ๋ ํ๋ง ๋จ๊ธด๋ค.
groupby('postal_flg').agg({'customer_id':'nunique'}) ๋ณํฉํ ๋ฐ์ดํฐ ํ๋ ์์ postal_flg๋ก ๊ทธ๋ฃนํํ๊ณ , nunique ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ทธ๋ฃน ๋ด ๊ณ ์ ํ customer_id ๊ฐ์ ๊ฐ์๋ฅผ ๊ณ์ฐํฉ๋๋ค. ๊ฐ postal_flg์ ๋ํด ํ ์ค์ ๋ฐ์ดํฐ ํ๋ ์์ ๋ฐํํ์ฌ ๊ฐ ๊ทธ๋ฃน์ ๊ณ ๊ฐ ์๋ฅผ ํ์ํฉ๋๋ค.
P-054: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ์ฃผ์(address)๋ ์ฌ์ดํ๋งํ, ์ง๋ฐํ, ๋์ฟ๋, ๊ฐ๋๊ฐ์ํ ์ค ํ๋์ด๋ค. ๋๋๋ถํ๋ณ๋ก ์ฝ๋ ๊ฐ์ ์์ฑํ์ฌ ๊ณ ๊ฐ ID, ์ฃผ์์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ผ. ๊ฐ์ ์ฌ์ดํ๋งํ์ 11, ์ง๋ฐํ์ 12, ๋์ฟ๋๋ฅผ 13, ๊ฐ๋๊ฐ์ํ์ 14๋ก ํ๋ค.
# ์ฝ๋ ์์ 1 (๊ณ ์ ์ผ๋ก ์๋ผ๋ด๊ธฐ)
df_customer_tmp = df_customer[['customer_id', 'address']].copy()
df_customer_tmp['prefecture_cd'] = \
df_customer['address'].str[0:3].map({'ๅผ็็': '11',
'ๅ่็':'12',
'ๆฑไบฌ้ฝ':'13',
'็ฅๅฅๅท':'14'})
df_customer_tmp.head(10)
ย | customer_id | address | prefecture_cd |
---|---|---|---|
0 | CS021313000114 | ็ฅๅฅๅท็ไผๅขๅๅธ็ฒ็ชช********** | 14 |
1 | CS037613000071 | ๆฑไบฌ้ฝๆฑๆฑๅบๅ็ ********** | 13 |
2 | CS031415000172 | ๆฑไบฌ้ฝๆธ่ฐทๅบไปฃใ ๆจ********** | 13 |
3 | CS028811000001 | ็ฅๅฅๅท็ๆจชๆตๅธๆณๅบๅๆณ็บ********** | 14 |
4 | CS001215000145 | ๆฑไบฌ้ฝๅคง็ฐๅบไปฒๅ ญ้ท********** | 13 |
5 | CS020401000016 | ๆฑไบฌ้ฝๆฟๆฉๅบ่ฅๆจ********** | 13 |
6 | CS015414000103 | ๆฑไบฌ้ฝๆฑๆฑๅบๅ็ ********** | 13 |
7 | CS029403000008 | ๅ่็ๆตฆๅฎๅธๆตทๆฅฝ********** | 12 |
8 | CS015804000004 | ๆฑไบฌ้ฝๆฑๆฑๅบๅ็ ********** | 13 |
9 | CS033513000180 | ็ฅๅฅๅท็ๆจชๆตๅธๆญๅบๅ้จ็บ********** | 14 |
ํด์ค:
์ด ์ฝ๋๋ df_customer DataFrame์ address ์ด์์ ๋๋๋ถํ ์ฝ๋๋ฅผ ์ถ์ถํ์ฌ 'customer_id', 'address', 'prefecture_cd' ์ด์ ๊ฐ์ง ์๋ก์ด DataFrame df_customer_tmp๋ฅผ ์์ฑํ๊ณ ์์ต๋๋ค.
์ด ์ฝ๋์์๋ ๋จผ์ copy() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ df_customer DataFrame์ 'customer_id'์ 'address' ์ด์ ๋ณต์ฌํ์ฌ df_customer_tmp ๋ณ์์ ๋์ ํ๊ณ ์์ต๋๋ค.
df_customer_tmp = df_customer[['customer_id', 'address']].copy()
๋ค์์ผ๋ก str ์ ๊ทผ์๋ฅผ ์ฌ์ฉํ์ฌ 'address' ์ด์ ์ฒซ 3๊ธ์๋ฅผ ์ถ์ถํ๊ณ map() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ์ด 'prefecture_cd'์ ๋์ ํ๋ ์ฝ๋์ ๋๋ค.
df_customer_tmp['prefecture_cd'] = df_customer['address'].str[0:3].map({'์ฌ์ดํ๋งํ': '11', '์ง๋ฐํ': '12', '๋์ฟ๋': '13', '14'})
map() ๋ฉ์๋๋ ๊ฐ ๋๋๋ถํ ์ด๋ฆ์ ํด๋น ๋๋๋ถํ ์ฝ๋์ ๋งคํํ๋ค. ๊ฒฐ๊ณผ DataFrame df_customer_tmp๋ 'customer_id', 'address', 'prefecture_cd' ์ปฌ๋ผ์ ๊ฐ์ง๋ค.
# ์ฝ๋ ์์ 2 (์ ๊ท ํํ์ ์ฌ์ฉ)
df_customer_tmp = df_customer[['customer_id', 'address']].copy()
df_customer_tmp['prefecture_cd'] = \
df_customer['address'].str.extract(r'(^.*?[้ฝ้ๅบ็])')[0].\
map({'ๅผ็็': '11',
'ๅ่็':'12',
'ๆฑไบฌ้ฝ':'13',
'็ฅๅฅๅท็':'14'})
df_customer_tmp.head(10)
ย | customer_id | address | prefecture_cd |
---|---|---|---|
0 | CS021313000114 | ็ฅๅฅๅท็ไผๅขๅๅธ็ฒ็ชช********** | 14 |
1 | CS037613000071 | ๆฑไบฌ้ฝๆฑๆฑๅบๅ็ ********** | 13 |
2 | CS031415000172 | ๆฑไบฌ้ฝๆธ่ฐทๅบไปฃใ ๆจ********** | 13 |
3 | CS028811000001 | ็ฅๅฅๅท็ๆจชๆตๅธๆณๅบๅๆณ็บ********** | 14 |
4 | CS001215000145 | ๆฑไบฌ้ฝๅคง็ฐๅบไปฒๅ ญ้ท********** | 13 |
5 | CS020401000016 | ๆฑไบฌ้ฝๆฟๆฉๅบ่ฅๆจ********** | 13 |
6 | CS015414000103 | ๆฑไบฌ้ฝๆฑๆฑๅบๅ็ ********** | 13 |
7 | CS029403000008 | ๅ่็ๆตฆๅฎๅธๆตทๆฅฝ********** | 12 |
8 | CS015804000004 | ๆฑไบฌ้ฝๆฑๆฑๅบๅ็ ********** | 13 |
9 | CS033513000180 | ็ฅๅฅๅท็ๆจชๆตๅธๆญๅบๅ้จ็บ********** | 14 |
ํด์ค:
์ด ์ฝ๋๋ ๊ธฐ์กด DataFrame df_customer์์ customer_id์ address ์ด์ ์ ํํ์ฌ ์๋ก์ด DataFrame df_customer_tmp๋ฅผ ์์ฑํฉ๋๋ค.
๋ค์์ผ๋ก ์ ๊ท์์ ์ฌ์ฉํ์ฌ address ์ด์์ ๋๋๋ถํ ์ฝ๋๋ฅผ ์ถ์ถํ๊ณ ์์ต๋๋ค. ๊ตฌ์ฒด์ ์ผ๋ก str.extract() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํจํด(^. *? [๋๋๋ถํ])์ ์ผ์นํ๋ ๋ถ๋ถ ๋ฌธ์์ด์ ์ถ์ถํฉ๋๋ค. ์ด๋ "๋ฌธ์์ด์ ์์๋ถํฐ '้ฝ' ๋๋ '้' ๋๋ 'ๅบ' ๋๋ '็'์ด ์ฒ์ ๋ํ๋ ๋๊น์ง์ ์์์ ๋ฌธ์์ ์ผ์นํ๋ ๊ฒ"์ ์๋ฏธํ๋ค.
๋ค์์ผ๋ก ์ถ์ถ๋ ๋๋๋ถํ ์ด๋ฆ์ ํด๋น ๋๋๋ถํ ์ฝ๋์ ๋งคํํ์ฌ df_customer_tmp DataFrame์ prefecture_cd๋ผ๋ ์๋ก์ด ์ด์ ์์ฑํ๋ค. ๋งคํ์ ๋๋๋ถํ ์ด๋ฆ์ ํค๋ก, ํด๋น ๋๋๋ถํ ์ฝ๋๋ฅผ ๊ฐ์ผ๋ก ํ๋ ์ฌ์ ์ ์ด์ฉํ map() ๋ฉ์๋๋ฅผ ํตํด ์ด๋ฃจ์ด์ง๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก df_customer_tmp๋ customer_id, address, prefecture_cd์ ์ธ ๊ฐ์ ์ปฌ๋ผ์ ๊ฐ์ง๋ฉฐ, prefecture_cd๋ ๋๋๋ถํ ์ฝ๋๋ฅผ ๋ํ๋ด๋ 2์๋ฆฌ ๋ฌธ์์ด๋ก DataFrame์ ์ฒซ 10๊ฐ์ ํ์ ํ์ํ๊ธฐ ์ํด head( 10) ๋ฉ์๋๊ฐ ์ฌ์ฉ๋์๋ค.
ย
P-055: ์์์ฆ ๋ช ์ธ์(df_receipt) ๋ฐ์ดํฐ์ ๋งค์ถ ๊ธ์ก(amount)์ ๊ณ ๊ฐ ID(customer_id)๋ณ๋ก ํฉ์ฐํ๊ณ , ๊ทธ ํฉ์ฐ ๊ธ์ก์ ์ฌ๋ถ์์๋ฅผ ๊ตฌํ์์ค. ๊ทธ ํ, ๊ณ ๊ฐ๋ณ ๋งค์ถ๊ธ์ก ํฉ๊ณ์ ๋ํด ์๋ ๊ธฐ์ค์ผ๋ก ์นดํ ๊ณ ๋ฆฌ ๊ฐ์ ์์ฑํ์ฌ ๊ณ ๊ฐ ID, ๋งค์ถ๊ธ์ก ํฉ๊ณ์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ผ. ์นดํ ๊ณ ๋ฆฌ ๊ฐ์ ์์๋๋ก 1~4๋ก ํ๋ค.
- ์ต์๊ฐ ์ด์ 1์ฌ๋ถ์์ ๋ฏธ๋ง ใปใปใป 1์ ๋ถ์ฌ
- 1์ฌ๋ถ์ ์ด์ 2์ฌ๋ถ์ ๋ฏธ๋ง ใปใปใป 2๋ฅผ ๋ถ์ฌ
- 2์ฌ๋ถ์ ์ด์ 3์ฌ๋ถ์ ๋ฏธ๋ง ใปใปใป 3์ ๋ถ์ฌ
- 3์ฌ๋ถ์ ์ด์ ใปใปใป 4์ ๋ถ์ฌ
ย
# ์ฝ๋ ์์ 1
df_sales_amount = df_receipt[['customer_id', 'amount']]. \
groupby('customer_id').sum().reset_index()
pct25 = np.quantile(df_sales_amount['amount'], 0.25)
pct50 = np.quantile(df_sales_amount['amount'], 0.5)
pct75 = np.quantile(df_sales_amount['amount'], 0.75)
def pct_group(x):
if x < pct25:
return 1
elif pct25 <= x < pct50:
return 2
elif pct50 <= x < pct75:
return 3
elif pct75 <= x:
return 4
df_sales_amount['pct_group'] = df_sales_amount['amount'].apply(pct_group)
df_sales_amount.head(10)
ย | customer_id | amount | pct_group |
---|---|---|---|
0 | CS001113000004 | 1298 | 2 |
1 | CS001114000005 | 626 | 2 |
2 | CS001115000010 | 3044 | 3 |
3 | CS001205000004 | 1988 | 3 |
4 | CS001205000006 | 3337 | 3 |
5 | CS001211000025 | 456 | 1 |
6 | CS001212000027 | 448 | 1 |
7 | CS001212000031 | 296 | 1 |
8 | CS001212000046 | 228 | 1 |
9 | CS001212000070 | 456 | 1 |
ํด์ค:
์ด ์ฝ๋๋ ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก ์ฌ๋ถ์์๋ฅผ ๊ณ์ฐํ๊ณ , ๋งค์ถ ๊ธ์ก ์ฌ๋ถ์์์ ๋ฐ๋ผ ๊ฐ ๊ณ ๊ฐ์ 4๊ฐ์ ๊ทธ๋ฃน ์ค ํ๋์ ํ ๋นํ๋ค.
๋ค์์ ์ฝ๋์ ๋จ๊ณ๋ณ ๋ถ์์ด๋ค.
df_sales_amount = df_receipt[['customer_id', 'amount']].groupby('customer_id').sum().reset_index() df_receipt ๋ฐ์ดํฐํ๋ ์์ customer_id๋ก ๊ทธ๋ฃนํ ํ์ฌ ๊ธ์ก ์ด์ ํฉ์ฐํ์ฌ ๊ฐ ๊ณ ๊ฐ์ ์ด ๋งค์ถ ๊ธ์ก์ ๊ณ์ฐํ๋ค. ๊ฒฐ๊ณผ dataframe์ customer_id์ amount ๋ ๊ฐ์ ์ปฌ๋ผ์ ๊ฐ์ง๋ค.
pct25 = np.quantile(df_sales_amount['amount'], 0.25), pct50 = np.quantile(df_sales_amount['amount'], 0.5), pct75 = np.quantile(df_sales_amount['amount'], 0.5), pct75 = np.quantile(df_sales_amount['amount amount['amount'], 0.75) ๋งค์ถ์ก ๋ถํฌ์ 25, 50, 75%๋ฅผ ๊ณ์ฐํ๋ค.
def pct_group(x): ... ๋งค์ถ ๊ธ์ก x๋ฅผ ๋ฐ์ x๊ฐ ์ํ ์ฌ๋ถ์๋ฅผ ๋ํ๋ด๋ 1๋ถํฐ 4๊น์ง์ ์ ์๋ฅผ ๋ฐํํ๋ ํจ์ pct_group์ ์ ์ํ๋ค.
df_sales_amount['pct_group'] = df_sales_amount['amount'].apply(pct_group) df_sales_amount ์ amount ์ด์ ๊ฐ ๊ฐ์ pct_group ํจ์๋ฅผ ์ ์ฉํ๊ณ , ๊ฒฐ๊ณผ ์ฌ๋ถ์์๋ฅผ pct_group์ด๋ผ๋ ์๋ก์ด ์ด์ ๋์ ํฉ๋๋ค. group์ด๋ผ๋ ์๋ก์ด ์ด์ ํ ๋นํฉ๋๋ค.
df_sales_amount.head(10)๋ customer_id, amount, pct_group์ ์ธ ๊ฐ์ ์ปฌ๋ผ์ ๊ฐ์ง ๊ฒฐ๊ณผ ๋ฐ์ดํฐ ํ๋ ์์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํฉ๋๋ค.
# ํ์ธ์ฉ ์ฝ๋
print('pct25:', pct25)
print('pct50:', pct50)
print('pct75:', pct75)
pct25: 548.5 pct50: 1478.0 pct75: 3651.0
# ์ฝ๋ ์์ 2 (cut์ ์ฌ์ฉํ ์์, ์ฌ๋ถ์์ ๋ฒ์๋ ์ฐธ๊ณ ์ฉ์ผ๋ก ์ถ๊ฐ ํ์)
df_temp = df_receipt[['customer_id', 'amount']]. \
groupby('customer_id').sum().reset_index()
pct25 = np.quantile(df_sales_amount['amount'], 0.25)
pct50 = np.quantile(df_sales_amount['amount'], 0.5)
pct75 = np.quantile(df_sales_amount['amount'], 0.75)
pct_max = df_sales_amount['amount'].max()
df_temp['quantile'] = pd.cut(df_sales_amount['amount'],[0.0, pct25, pct50, pct75,pct_max+0.1], right=False)
df_temp['pct_group'] = df_temp.groupby('quantile').ngroup() + 1
df_temp.head(10)
ย | customer_id | amount | quantile | pct_group |
---|---|---|---|---|
0 | CS001113000004 | 1298 | [548.5, 1478.0) | 2 |
1 | CS001114000005 | 626 | [548.5, 1478.0) | 2 |
2 | CS001115000010 | 3044 | [1478.0, 3651.0) | 3 |
3 | CS001205000004 | 1988 | [1478.0, 3651.0) | 3 |
4 | CS001205000006 | 3337 | [1478.0, 3651.0) | 3 |
5 | CS001211000025 | 456 | [0.0, 548.5) | 1 |
6 | CS001212000027 | 448 | [0.0, 548.5) | 1 |
7 | CS001212000031 | 296 | [0.0, 548.5) | 1 |
8 | CS001212000046 | 228 | [0.0, 548.5) | 1 |
9 | CS001212000070 | 456 | [0.0, 548.5) | 1 |
ํด์ค:
์ด ์ฝ๋์์๋ DataFrame 'df_temp'์ ์๋ก์ด ์ด 'pct_group'์ ์์ฑํ์ฌ ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก ํฉ๊ณ์ ๊ทธ ๋งค์ถ ๊ธ์ก ํฉ๊ณ๋ฅผ ๊ธฐ์ค์ผ๋ก ์์๋ ๋ฐฑ๋ถ์์ ๊ทธ๋ฃน์ ์ ์ฅํ๊ณ ์์ต๋๋ค.
์ฒซ ๋ฒ์งธ ํ์ 'df_receipt' DataFrame์ customer_id๋ก ๊ทธ๋ฃนํํ์ฌ ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ , customer_id๋ฅผ ๋ค์ ์ด๋ก ๋ง๋ค๊ธฐ ์ํด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ๊ณ ์์ต๋๋ค.
๋ค์ 4ํ์ ์์ ์์ฑํ 'df_temp' DataFrame๊ณผ ๋์ผํ 'df_sales_amount' DataFrame์์ ๋งค์ถ ๊ธ์ก์ 25%, 50%, 75%, ์ต๋๊ฐ์ ๊ณ์ฐํ๊ณ ์์ต๋๋ค.
๋ค์ ํ์์๋ ์ ์๋ ๋ฐฑ๋ถ์์ ๊ฐ์ผ๋ก ๋งค์ถ์ก์ 4๊ฐ์ ๋น์ผ๋ก ๊ตฌ๋ถํ์ฌ 'df_temp'์ ์๋ก์ด ์ด 'quantile'์ ์์ฑํฉ๋๋ค.
๋ง์ง๋ง ํ์์๋ 'df_temp'์ ์๋ก์ด ์ด 'pct_group'์ ์์ฑํ์ฌ 'quantile'์ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋ฃนํํ๊ณ , ๊ฐ ๊ณ ๊ฐ์ด ์ด๋ ๋ฐฑ๋ถ์์ ๊ทธ๋ฃน์ ์ํด ์๋์ง์ ๋ฐ๋ผ ๊ทธ๋ฃน ๋ฒํธ(1-4)๋ฅผ ๋ถ์ฌํ๊ณ ์์ต๋๋ค.
์ฐธ๊ณ ๋ก ๋ง์ง๋ง ๋น์ ์ค๋ฅธ์ชฝ ๋์ 'pct_max+0.1'๋ก ์ ์ํ์ฌ ๋งค์ถ ๊ธ์ก์ ์ต๋๊ฐ์ด ๋ง์ง๋ง ๋ฐฑ๋ถ์์ ๊ทธ๋ฃน์ ํฌํจ๋๋๋ก ํ์์ต๋๋ค.
# ์ฐธ๊ณ ์ฝ๋ (qcut์ ์ฌ์ฉํ ์์, ๊ฒฝ๊ณ๊ฐ ํฌํจ/๋ฏธํฌํจ ์ฌ๋ถ๊ฐ ๋ฐ๋๋ก ๋์ด ์์ด ์ ๋ชฉ์ ์ถฉ์กฑ์ํค์ง ๋ชปํ์ง๋ง ์ฐธ๊ณ ์ฉ์ผ๋ก ๊ธฐ์ฌ)
df_temp = df_receipt.groupby('customer_id')[['amount']].sum()
df_temp['quantile'], bins = \
pd.qcut(df_receipt.groupby('customer_id')['amount'].sum(), 4, retbins=True)
df_temp['pct_group'] = df_temp.groupby('quantile').ngroup() + 1
df_temp.reset_index(inplace=True)
display(df_temp.head(10))
print('quantiles:', bins)
ย | customer_id | amount | quantile | pct_group |
---|---|---|---|---|
0 | CS001113000004 | 1298 | (548.5, 1478.0] | 2 |
1 | CS001114000005 | 626 | (548.5, 1478.0] | 2 |
2 | CS001115000010 | 3044 | (1478.0, 3651.0] | 3 |
3 | CS001205000004 | 1988 | (1478.0, 3651.0] | 3 |
4 | CS001205000006 | 3337 | (1478.0, 3651.0] | 3 |
5 | CS001211000025 | 456 | (69.999, 548.5] | 1 |
6 | CS001212000027 | 448 | (69.999, 548.5] | 1 |
7 | CS001212000031 | 296 | (69.999, 548.5] | 1 |
8 | CS001212000046 | 228 | (69.999, 548.5] | 1 |
9 | CS001212000070 | 456 | (69.999, 548.5] | 1 |
quantiles: [7.0000000e+01 5.4850000e+02 1.4780000e+03 3.6510000e+03 1.2395003e+07]
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํ๋ค.
df_receipt ๋ฐ์ดํฐ ํ๋ ์์ customer_id๋ก ๊ทธ๋ฃนํํ๊ณ , grouped ๊ฐ์ฒด์ sum() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ทธ๋ฃน์ ๊ธ์ก ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๋ค. ๊ทธ ๊ฒฐ๊ณผ๋ df_temp๋ผ๋ ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์์ ์ ์ฅ๋๋ค.
qcut() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ df_temp์ ๊ธ์ก ๊ฐ์ ๊ธฐ์ค์ผ๋ก 4 ๊ฐ์ ๋์ผํ ํฌ๊ธฐ์ ๋น์ผ๋ก ๋ถํ ํ๊ณ retbins=True ์ต์ ์ ๋น์ ๋์ bins ๋ณ์์ ๋ฐํํ๋ค.
df_temp์ quantile์ด๋ผ๋ ์๋ก์ด ์ด์ด ์์ฑ๋์ด ๊ฐ ๊ธ์ก ๊ฐ์ด ์ด๋ค ๋น์ ๋ค์ด๊ฐ์ง ์๋ ค์ค๋ค.
ngroup() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ df_temp['quantile']์ ๊ฐ ๋น์ ๊ทธ๋ฃน ๋ฒํธ๋ฅผ ํ ๋นํ๊ณ , ngroup()์ 0 ์ธ๋ฑ์ค ๊ฐ์ ๋ฐํํ๋ฏ๋ก ๊ฒฐ๊ณผ์ +1์ด ์ถ๊ฐ๋ฉ๋๋ค.
reset_index() ๋ฉ์๋๊ฐ df_temp์์ ํธ์ถ๋์ด ์ธ๋ฑ์ค๊ฐ ์๋ก์ด ๋ฒ์ ์ธ๋ฑ์ค๋ก ์ฌ์ค์ ๋๋ค.
๋ง์ง๋ง์ผ๋ก df_temp์ ์ฒ์ 10ํ๊ณผ ๋น์ ๋์ด ๊ฐ๊ฐ display()์ print() ํจ์๋ก ํ์๋๋ค.
ย
P-056: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ๋์ด(age)๋ฅผ ๊ธฐ์ค์ผ๋ก 10์ธ ๋จ์๋ก ์ฐ๋ น์ ๊ณ์ฐํ์ฌ ๊ณ ๊ฐ ID(customer_id), ์๋ ์์ผ(birth_day)๊ณผ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ค. ๋จ, 60์ธ ์ด์์ ๋ชจ๋ 60๋ ์ด์์ผ๋ก ํ๋ค. ์ฐ๋ น์ ๋ํ๋ด๋ ์นดํ ๊ณ ๋ฆฌ ๋ช ์นญ์ ์์๋๋ก ํ๋ค.
# ์ฝ๋ ์์ 1
df_customer_era = df_customer[['customer_id', 'birth_day']].copy()
df_customer_era['era'] = df_customer['age']. \
apply(lambda x: min(math.floor(x / 10) * 10, 60))
df_customer_era.head(10)
ย | customer_id | birth_day | era |
---|---|---|---|
0 | CS021313000114 | 1981-04-29 | 30 |
1 | CS037613000071 | 1952-04-01 | 60 |
2 | CS031415000172 | 1976-10-04 | 40 |
3 | CS028811000001 | 1933-03-27 | 60 |
4 | CS001215000145 | 1995-03-29 | 20 |
5 | CS020401000016 | 1974-09-15 | 40 |
6 | CS015414000103 | 1977-08-09 | 40 |
7 | CS029403000008 | 1973-08-17 | 40 |
8 | CS015804000004 | 1931-05-02 | 60 |
9 | CS033513000180 | 1962-07-11 | 50 |
ํด์ค:
์ด ์ฝ๋์์๋ customer_id, birth_day, era ์ปฌ๋ผ์ ํฌํจํ๋ ์๋ก์ด DataFrame df_customer_era๋ฅผ ์์ฑํ๋ค.
customer_id์ birth_day ์ปฌ๋ผ์ ์๋์ df_customer DataFrame์์ ๊ฐ์ ธ์จ๋ค.
age ์ด์ df_customer DataFrame์ age ์ด์์ ๊ณ์ฐ๋๋ฉฐ, age์ ๊ฐ ๊ฐ์ ๋ํด ์ฝ๋๋ ๊ฐ์ ๋ฐ์ฌ๋ฆผํ 10๋ (10์ ๋ฐฐ์)์ ๊ณ์ฐํ๊ณ , age์ ๊ฐ์ด 60๋ณด๋ค ํฌ๋ฉด ์ฐ๋๋ 60์ผ๋ก ์ค์ ๋๋ค.
apply ๋ฉ์๋์์ age ์ด์ ๊ฐ ์์์ ํจ์(์ฌ๊ธฐ์๋ lambda ํจ์)๋ฅผ ์ ์ฉํ๊ณ min ํจ์์ math.floor ํจ์๋ก 10๋ ์ ๊ณ์ฐํ๋ค. ๊ฒฐ๊ณผ ๊ฐ์ df_customer_era์ era ์ด์ ํ ๋น๋๋ค.
๋ง์ง๋ง์ผ๋ก head ๋ฉ์๋๊ฐ ํธ์ถ๋์ด ๊ฒฐ๊ณผ DataFrame์ ์ฒซ 10ํ์ด ํ์๋๋ค.
# ์ฝ๋ ์์ 2 (cut์ ์, ์นดํ
๊ณ ๋ฆฌ๋ ๋ฒ์๋ก ์ถ๋ ฅ)
df_customer_era = df_customer[['customer_id', 'birth_day']].copy()
df_customer_era['era'] = pd.cut(df_customer['age'],
bins=[0, 10, 20, 30, 40, 50, 60, np.inf],
right=False)
df_customer_era[['customer_id', 'birth_day', 'era']].head(10)
ย | customer_id | birth_day | era |
---|---|---|---|
0 | CS021313000114 | 1981-04-29 | [30.0, 40.0) |
1 | CS037613000071 | 1952-04-01 | [60.0, inf) |
2 | CS031415000172 | 1976-10-04 | [40.0, 50.0) |
3 | CS028811000001 | 1933-03-27 | [60.0, inf) |
4 | CS001215000145 | 1995-03-29 | [20.0, 30.0) |
5 | CS020401000016 | 1974-09-15 | [40.0, 50.0) |
6 | CS015414000103 | 1977-08-09 | [40.0, 50.0) |
7 | CS029403000008 | 1973-08-17 | [40.0, 50.0) |
8 | CS015804000004 | 1931-05-02 | [60.0, inf) |
9 | CS033513000180 | 1962-07-11 | [50.0, 60.0) |
ํด์ค :
์ด ์ฝ๋์์๋ customer_id, birth_day, era ์ปฌ๋ผ์ ๊ฐ์ง ์๋ก์ด DataFrame df_customer_era๋ฅผ ์์ฑํ๋ฉฐ, customer_id์ birth_day ์ปฌ๋ผ์ ์๋์ df_customer DataFrame์์ ๋ณต์ฌ๋ ๊ฒ์ ๋๋ค.
era ์ด์ pd.cut()์ ์ฌ์ฉํ์ฌ ๊ณ ๊ฐ์ ์ฐ๋ น์ ๋ค๋ฅธ ์ฐ๋ น๋๋ก ๋น๋ํ์ฌ ์์ฑ๋๋ฉฐ, bins ๋งค๊ฐ ๋ณ์๋ ๊ฐ ๋น์ ๋์ ์ง์ ํ๊ณ , np.inf๋ 60์ธ ์ด์์ ๊ณ ๊ฐ์ ๋ํ ์คํ ์๋ ๋น์ ์ง์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ๋งค๊ฐ ๋ณ์๋ False๋ก ์ค์ ๋์ด ๊ตฌ๊ฐ์ด ์ผ์ชฝ๋ถํฐ ๋ค์ด๊ฐ ์ ์๋๋ก ์ง์ ํฉ๋๋ค. ์ฆ, ๋์ด๊ฐ ์ ํํ 30์ธ์ธ ๊ณ ๊ฐ์ [30, 40] ๋น์ ๋ฐฐ์น๋๋ ๊ฒ์ด๋ค.
DataFrame์ ๊ฐ ํ์ ๋ํด ๊ณ ๊ฐ์ ์ฐ๋ น์ด ์ง์ ๋ ๋น์ ๋ฐ๋ผ ํด๋น ์ฐ๋ น์ ํด๋นํ๋ ๋น์ ์ฑ์์ง๋ค. ๊ฒฐ๊ณผ DataFrame์๋ customer_id, birth_day, era์ ๊ฐ ์ปฌ๋ผ์ด ํฌํจ๋๋ฉฐ, era ์ปฌ๋ผ์๋ ๊ฐ ๊ณ ๊ฐ์ ์ฐ๋ น๋๋ณ ๋น์ด ํฌํจ๋๋ค.
ย
P-057: 056์ ์ถ์ถ ๊ฒฐ๊ณผ์ ์ฑ๋ณ ์ฝ๋(gender_cd)์ ๋ฐ๋ผ ์ฑ๋ณร์ฐ๋ น์ ์กฐํฉ์ ๋ํ๋ด๋ ์นดํ ๊ณ ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์๋ก ๋ง๋ค์ด 10๊ฐ๋ฅผ ํ์ํ๋ผ. ์กฐํฉ์ ๋ํ๋ด๋ ์นดํ ๊ณ ๋ฆฌ์ ๊ฐ์ ์์๋ก ์ ํ๋ค.
# ์ฑ๋ณ ์ฝ๋ 1์๋ฆฌ์ ์ฐ๋ ์ฝ๋ 2์๋ฆฌ๋ฅผ ์ฐ๊ฒฐํ ์ฑ์ฐ๋ น ์ฝ๋๋ฅผ ์์ฑํ๋ค.
df_customer_era = df_customer[['customer_id', 'birth_day']].copy()
df_customer_era['era'] = df_customer['age']. \
apply(lambda x: min(math.floor(x / 10) * 10, 60))
df_customer_era['gender_era'] = \
df_customer['gender_cd'] + df_customer_era['era'].astype('str').str.zfill(2)
df_customer_era.head(10)
ย | customer_id | birth_day | era | gender_era |
---|---|---|---|---|
0 | CS021313000114 | 1981-04-29 | 30 | 130 |
1 | CS037613000071 | 1952-04-01 | 60 | 960 |
2 | CS031415000172 | 1976-10-04 | 40 | 140 |
3 | CS028811000001 | 1933-03-27 | 60 | 160 |
4 | CS001215000145 | 1995-03-29 | 20 | 120 |
5 | CS020401000016 | 1974-09-15 | 40 | 040 |
6 | CS015414000103 | 1977-08-09 | 40 | 140 |
7 | CS029403000008 | 1973-08-17 | 40 | 040 |
8 | CS015804000004 | 1931-05-02 | 60 | 060 |
9 | CS033513000180 | 1962-07-11 | 50 | 150 |
ํด์ค:ย
์ด ์ฝ๋๋ DataFrame "df_customer_era"์ ์๋ก์ด ์ด "era"๋ฅผ ์์ฑํ์ฌ ๊ณ ๊ฐ ์ฐ๋ น์ 10 ๋ ์ ํ์ํ๋ฉฐ, decade์ ๊ฐ์ ์ฐ๋ น์ 10 ์ธ ๋ฏธ๋ง์ผ๋ก ๋ฐ์ฌ๋ฆผ ํ ํ 10์ ๊ณฑํ์ฌ ์ป์ต๋๋ค. ๊ฒฐ๊ณผ๊ฐ 60๋ณด๋ค ํฌ๋ฉด 60์ผ๋ก ๋ฐ์ฌ๋ฆผํฉ๋๋ค.
๊ทธ๋ฐ ๋ค์ ์ฝ๋๋ ์ฑ๋ณ ์ฝ๋์ "์๋"์ด์ ๊ฐ์ ์ฐ๊ฒฐํ์ฌ ์๋ก์ด "gender_era"์ด์ ์์ฑํ๊ณ "gender_cd"์ด์๋ ์ฑ๋ณ ์ฝ๋ (์ : ๋จ์ฑ์ "0", ์ฌ์ฑ์ "1")๊ฐ ํฌํจ๋๋ฉฐ "era"์ด์ ๊ฐ์ ๋ฌธ์์ด๋ก ์บ์คํ ๋๊ณ ๋๋น 2์ ๋ง๊ฒ 0์ผ๋ก ํจ๋ฉ๋ฉ๋๋ค. ๋๋ค.
๋ง์ง๋ง์ผ๋ก ์ด ์ฝ๋๋ ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10์ค์ ํ์ํ๋ค.
P-058: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ์ฑ๋ณ ์ฝ๋(gender_cd)๋ฅผ ๋๋ฏธ ๋ณ์๋ก ๋ง๋ค์ด ๊ณ ๊ฐ ID(customer_id)์ ํจ๊ป 10๊ฑด ํ์ํ๋ผ.
# ์ฝ๋ ์์ 1 (๋ชจ๋ ์ฝ๋ ๊ฐ์ ํญ๋ชฉํ)
pd.get_dummies(df_customer[['customer_id', 'gender_cd']],
columns=['gender_cd']).head(10)
ย | customer_id | gender_cd_0 | gender_cd_1 | gender_cd_9 |
---|---|---|---|---|
0 | CS021313000114 | 0 | 1 | 0 |
1 | CS037613000071 | 0 | 0 | 1 |
2 | CS031415000172 | 0 | 1 | 0 |
3 | CS028811000001 | 0 | 1 | 0 |
4 | CS001215000145 | 0 | 1 | 0 |
5 | CS020401000016 | 1 | 0 | 0 |
6 | CS015414000103 | 0 | 1 | 0 |
7 | CS029403000008 | 1 | 0 | 0 |
8 | CS015804000004 | 1 | 0 | 0 |
9 | CS033513000180 | 0 | 1 | 0 |
ํด์ค:
์ด ์ฝ๋๋ DataFrame 'df_customer'์ ์นดํ ๊ณ ๋ฆฌ ๋ณ์ 'gender_cd'์ ๋๋ฏธ ๋ณ์๋ฅผ ์์ฑํ๋ค. ๊ฒฐ๊ณผ DataFrame์ 'gender_cd'์ ๊ฐ ์นดํ ๊ณ ๋ฆฌ(์๋ง๋ ๋จ์ฑ๊ณผ ์ฌ์ฑ)์ ์ด์ ๊ฐ์ง๋ฉฐ, ๊ฐ ํ์ ์๋์ ๊ณ ๊ฐ ๋ ์ฝ๋๊ฐ 'gender_cd'์ ํด๋น ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ๊ฒฝ์ฐ ํด๋น ์ด์ 1์, ๋ค๋ฅธ ๋ชจ๋ ์ด์ 0์ ๊ฐ์ง๊ฒ ๋๋ค.
์ด๋ฅผ ์ํด pandas์ 'get_dummies' ํจ์๊ฐ ์ฌ์ฉ๋๋ค. ์ด ํจ์๋ DataFrame๊ณผ ๋๋ฏธ ์ธ์ฝ๋ฉ์ ์ ์ฉํ ์ปฌ๋ผ ๋ชฉ๋ก์ ๋ฐ๋๋ค. ์ด ๊ฒฝ์ฐ 'df_customer' DataFrame์ ๋ํด ํธ์ถ๋๋ฉฐ, 'columns' ํ๋ผ๋ฏธํฐ์ 'gender_cd' ์ปฌ๋ผ์ด ์ง์ ๋์ด ์๋ค.
๊ฒฐ๊ณผ DataFrame์ 'gender_cd'์ ๊ฐ ๊ฐ์ ๋ํด ํ๋์ ์ด์ ๊ฐ์ง๋ฉฐ(์๋ง๋ ์๋ณธ ๋ฐ์ดํฐ์ ์ฌ์ฉ๋ ์ธ์ฝ๋ฉ์ ๋ฐ๋ผ ๋จ์ฑ์ 0, ์ฌ์ฑ์ 1 ๋๋ ๊ทธ ๋ฐ๋๋ก), ๊ฐ ํ์ ํด๋น ํ์ 'gender_cd' ๊ฐ์ ํด๋นํ๋ ์ด์ 1์ ๊ฐ์ง๋ฉฐ, ๋ค๋ฅธ ๋ชจ๋ ์ด์๋ 0์ ๊ฐ์ง๊ฒ ๋๋ค. ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ด ๋ฐํ๋๋ค.
# ์ฝ๋ ์์ 2 (ํญ๋ชฉ์ ํ๋ ์ญ์ ํ๊ฑฐ๋ ๊ตฌ๋ถ ๋ฌธ์๋ฅผ ๋ฐ๊ฟ ์ ์์)
pd.get_dummies(df_customer[['customer_id', 'gender_cd']],
columns=['gender_cd'],
drop_first=True, prefix='gen', prefix_sep='#').head(10)
ย | customer_id | gen#1 | gen#9 |
---|---|---|---|
0 | CS021313000114 | 1 | 0 |
1 | CS037613000071 | 0 | 1 |
2 | CS031415000172 | 1 | 0 |
3 | CS028811000001 | 1 | 0 |
4 | CS001215000145 | 1 | 0 |
5 | CS020401000016 | 0 | 0 |
6 | CS015414000103 | 1 | 0 |
7 | CS029403000008 | 0 | 0 |
8 | CS015804000004 | 0 | 0 |
9 | CS033513000180 | 1 | 0 |
ํด์ค :ย
์ด ์ฝ๋๋ df_customer DataFrame์ ์นดํ ๊ณ ๋ฆฌ ๋ณ์ gender_cd์ ๋ํ ๋๋ฏธ ๋ณ์๋ฅผ ์์ฑํ๋ ์ฝ๋์ ๋๋ค. pandas ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ pd.get_dummies() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ gender_cd์ ๊ณ ์ ํ ์นดํ ๊ณ ๋ฆฌ๋ณ๋ก ๋ฐ์ด๋๋ฆฌ ์ธ๋ฑ์ค ๋ณ์๋ฅผ ์์ฑ ํฉ๋๋ค.
drop_first=True ์ธ์๋ ์ฐธ์กฐ ์นดํ ๊ณ ๋ฆฌ์ธ ์ฒซ ๋ฒ์งธ ์นดํ ๊ณ ๋ฆฌ('0')๋ฅผ ์ญ์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ด๋ ๋๋ฏธ ๋ณ์ ๊ฐ์ ๋ค์ค ๊ณต์ ์ฑ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ํ๋๋ค.
prefix='gen' ๋ฐ prefix_sep='#' ์ธ์๋ ๋๋ฏธ ๋ณ์์ ์ด ์ด๋ฆ์ ์ ๋์ฌ๋ฅผ ๋ถ์ด๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ด ๊ฒฝ์ฐ ์ ๋์ฌ๋ 'gen', ๊ตฌ๋ถ์๋ '#'์ด๋ค.
๊ฒฐ๊ณผ DataFrame์ gender_cd ์ด์ ๊ณ ์ ํ ๊ฐ๋ง๋ค ํ๋์ ์ด์ ๊ฐ์ง๋ฉฐ, ๊ทธ ๊ฐ์ด 1์ด๋ฉด ๊ณ ๊ฐ์ด ํด๋น ์นดํ ๊ณ ๋ฆฌ์ ์ํ๋ค๋ ๊ฒ์ ๋ํ๋ด๊ณ , 0์ด๋ฉด ์ํ์ง ์๋๋ค๋ ๊ฒ์ ๋ํ๋ธ๋ค. ์ปฌ๋ผ ์ด๋ฆ์ gen#<gender_cd> ํ์์ด๋ฉฐ, <gender_cd>๋ ์๋์ gender_cd ์ปฌ๋ผ์ ๊ณ ์ ํ ๊ฐ์์ ๋ํ๋ธ๋ค.
ย
P-059: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก(amount)์ ๊ณ ๊ฐ ID(customer_id)๋ณ๋ก ํฉ์ฐํ๊ณ , ๋งค์ถ ๊ธ์ก ํฉ๊ณ๋ฅผ ํ๊ท 0, ํ์คํธ์ฐจ 1๋ก ํ์คํํ์ฌ ๊ณ ๊ฐ ID, ๋งค์ถ ๊ธ์ก ํฉ๊ณ์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ผ. ํ์คํ์ ์ฌ์ฉํ๋ ํ์คํธ์ฐจ๋ ๋ถ์ฐ ์ ๊ณฑ๊ทผ ๋๋ ๋ถ๊ท ํ ๋ถ์ฐ ์ ๊ณฑ๊ทผ ์ค ์ด๋ ๊ฒ์ด๋ ์๊ด์๋ค. ๋จ, ๊ณ ๊ฐ ID๊ฐ โZโ๋ก ์์ํ๋ ๊ฒ์ ๋นํ์์ ์๋ฏธํ๋ฏ๋ก ์ ์ธํ์ฌ ๊ณ์ฐํ๋ค.
TIPS: query()์ ์ธ์ engine์์ 'python' ๋๋ 'numexpr'์ ์ ํํ ์ ์์ผ๋ฉฐ, ๊ธฐ๋ณธ๊ฐ์ numexpr์ด ์ค์น๋์ด ์์ผ๋ฉด numexpr์ด, ์ค์น๋์ด ์์ง ์์ผ๋ฉด python์ด ์ฌ์ฉ๋ฉ๋๋ค. ๋ํ, string ๋ฉ์๋๋ engine='python'์ด ์๋๋ฉด query() ๋ด์์ ์ฌ์ฉํ ์ ์๋ค.
# sklean์ preprocessing.scale์ ์ด์ฉํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ํ์คํธ์ฐจ๋ก ๊ณ์ฐ๋จ.
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")',
engine='python'). \
groupby('customer_id'). \
agg({'amount':'sum'}).reset_index()
df_sales_amount['std_amount'] = preprocessing.scale(df_sales_amount['amount'])
df_sales_amount.head(10)
ย | customer_id | amount | std_amount |
---|---|---|---|
0 | CS001113000004 | 1298 | -0.459378 |
1 | CS001114000005 | 626 | -0.706390 |
2 | CS001115000010 | 3044 | 0.182413 |
3 | CS001205000004 | 1988 | -0.205749 |
4 | CS001205000006 | 3337 | 0.290114 |
5 | CS001211000025 | 456 | -0.768879 |
6 | CS001212000027 | 448 | -0.771819 |
7 | CS001212000031 | 296 | -0.827691 |
8 | CS001212000046 | 228 | -0.852686 |
9 | CS001212000070 | 456 | -0.768879 |
ํด์ค:
์ด ์ฝ๋์์๋ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํฉ๋๋ค.
df_receipt์์ customer_id๊ฐ "Z"๋ก ์์ํ๋ ํ์ ํํฐ๋งํ๋ค.
๋๋จธ์ง ํ์ customer_id๋ก ๊ทธ๋ฃนํํ๊ณ agg ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ทธ๋ฃน์ ๊ธ์ก ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๋ค.
๊ฒฐ๊ณผ DataFrame์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ์ฌ customer_id๋ฅผ ์ผ๋ฐ ์ปฌ๋ผ์ผ๋ก ๋ง๋ ๋ค.
scikit-learn์ preprocessing.scale ํจ์๋ฅผ amount ์ด์ ์ ์ฉํ์ฌ ๊ฐ ๊ณ ๊ฐ์ ์ด ๋งค์ถ์ z ์ ์๋ฅผ ๊ณ์ฐํ๋ค.
DataFrame์ std_amount๋ผ๋ ์๋ก์ด ์ด์ ์ถ๊ฐํ๊ณ 4๋จ๊ณ์์ ๊ณ์ฐํ z์ ์๋ฅผ ์ ์ฅํ๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก DataFrame df_sales_amount์๋ ๊ฐ ๊ณ ๊ฐ์ ๋ํด ํ ํ์ด ํฌํจ๋๋ฉฐ, ๊ณ ๊ฐ ID, ์ด ๋งค์ถ ๊ธ์ก, ๋งค์ถ์ z ์ ์์ธ std_amount ์ด์ด ํฌํจ๋๋ค.
# ์ฝ๋ ์์ 2 (fit์ ํตํด ๋ค๋ฅธ ๋ฐ์ดํฐ์์๋ ๋์ผํ ํ๊ท ๋ฐ ํ์คํธ์ฐจ๋ก ํ์คํํ ์ ์์)
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")',
engine='python'). \
groupby('customer_id'). \
agg({'amount':'sum'}).reset_index()
scaler = preprocessing.StandardScaler()
scaler.fit(df_sales_amount[['amount']])
df_sales_amount['std_amount'] = scaler.transform(df_sales_amount[['amount']])
df_sales_amount.head(10)
ย | customer_id | amount | std_amount |
---|---|---|---|
0 | CS001113000004 | 1298 | -0.459378 |
1 | CS001114000005 | 626 | -0.706390 |
2 | CS001115000010 | 3044 | 0.182413 |
3 | CS001205000004 | 1988 | -0.205749 |
4 | CS001205000006 | 3337 | 0.290114 |
5 | CS001211000025 | 456 | -0.768879 |
6 | CS001212000027 | 448 | -0.771819 |
7 | CS001212000031 | 296 | -0.827691 |
8 | CS001212000046 | 228 | -0.852686 |
9 | CS001212000070 | 456 | -0.768879 |
ํด์ค:
์ด ์ฝ๋๋ 'df_sales_amount' DataFrame์ 'amount' ์ด์ ๋ํด ํ์คํ(Z ์ ์ ์ ๊ทํ๋ผ๊ณ ๋ ํจ)๋ฅผ ์ํํ๋ค.
์ด ์ฝ๋์์๋ ๋จผ์ pandas DataFrame์ 'query' ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ID๊ฐ 'Z'๋ก ์์ํ๋ ๊ณ ๊ฐ์ ํธ๋์ญ์ ์ ํํฐ๋งํ๊ณ ์๋ค. ๊ทธ๋ฐ ๋ค์ ๋๋จธ์ง ๊ฑฐ๋๋ฅผ 'customer_id'๋ก ๊ทธ๋ฃนํํ๊ณ 'agg' ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ณ ๊ฐ์ด ์ฌ์ฉํ ์ด ๊ธ์ก์ ๊ณ์ฐํฉ๋๋ค. ๋ง์ง๋ง์ผ๋ก ๊ฒฐ๊ณผ DataFrame์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ๊ณ ์์ต๋๋ค.
๋ค์์ผ๋ก ์ด ์ฝ๋๋ scikit-learn ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ 'preprocessing' ๋ชจ๋์์ 'StandardScaler' ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋๋ฐ, 'StandardScaler' ํด๋์ค๋ ํ๊ท ์ด 0, ํ์คํธ์ฐจ๊ฐ 1์ด ๋๋๋ก ๋ฐ์ดํฐ๋ฅผ ์ค์ผ์ผ๋งํ๊ณ ํ์คํ์ ์ฌ์ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ 'scaler' ์ธ์คํด์ค์ 'fit' ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ 'df_sales_amount' DataFrame์ 'amount' ์ด์ ํ๊ท ๊ณผ ํ์คํธ์ฐจ๋ฅผ ๊ณ์ฐํ๋ค.
๋ง์ง๋ง์ผ๋ก 'scaler' ์ธ์คํด์ค์ 'transform' ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ 'df_sales_amount' DataFrame์ 'amount' ์ปฌ๋ผ์ ๋ํด ์ค์ ํ์คํ๋ฅผ ์ํํ๊ณ , ๊ฒฐ๊ณผ๊ฐ์ 'std_amount'๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅํ๋ค. ํฉ๋๋ค.
๊ฒฐ๊ณผ DataFrame 'df_sales_amount'์๋ 'customer_id'์ ๊ฐ ๊ณ ๊ฐ์ด ์ฌ์ฉํ ์ด 'amount'์ ํ์คํ๋ 'std_amount' ์ปฌ๋ผ์ด ํฌํจ๋๋ค.
P-060: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก(amount)์ ๊ณ ๊ฐ ID(customer_id)๋ณ๋ก ํฉ์ฐํ์ฌ ๋งค์ถ ๊ธ์ก ํฉ๊ณ๋ฅผ ์ต์๊ฐ 0, ์ต๋๊ฐ 1๋ก ์ ๊ทํํ์ฌ ๊ณ ๊ฐ ID, ๋งค์ถ ๊ธ์ก ํฉ๊ณ์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ค. ๋จ, ๊ณ ๊ฐ ID๊ฐ โZโ๋ก ์์ํ๋ ๊ฒ์ ๋นํ์์ ์๋ฏธํ๋ฏ๋ก ์ ์ธํ์ฌ ๊ณ์ฐํ๋ค.
# ์ฝ๋ ์์ 1
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")',
engine='python'). \
groupby('customer_id'). \
agg({'amount':'sum'}).reset_index()
df_sales_amount['scale_amount'] = \
preprocessing.minmax_scale(df_sales_amount['amount'])
df_sales_amount.head(10)
ย | customer_id | amount | scale_amount |
---|---|---|---|
0 | CS001113000004 | 1298 | 0.053354 |
1 | CS001114000005 | 626 | 0.024157 |
2 | CS001115000010 | 3044 | 0.129214 |
3 | CS001205000004 | 1988 | 0.083333 |
4 | CS001205000006 | 3337 | 0.141945 |
5 | CS001211000025 | 456 | 0.016771 |
6 | CS001212000027 | 448 | 0.016423 |
7 | CS001212000031 | 296 | 0.009819 |
8 | CS001212000046 | 228 | 0.006865 |
9 | CS001212000070 | 456 | 0.016771 |
ํด์ค:
์ด ์ฝ๋๋ pandas์ DataFrame df_receipt์ ๋ํด ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ๋ฅผ ํ๋ ์ฝ๋์ ๋๋ค. ์๋๋ ์ฝ๋์ ๊ฐ ํ์ด ๋ฌด์์ ํ๋์ง ์ค๋ช ํฉ๋๋ค.
query() ๋ฉ์๋์ regex engine python์ ์ฌ์ฉํ์ฌ ๊ณ ๊ฐ ID๊ฐ "Z"๋ก ์์ํ๋ ํ์ ํํฐ๋งํฉ๋๋ค. ์ป์ DataFrame์ df_sales_amount์ ๋์ ํ๋ค.
groupby() ๋ฉ์๋๋ก DataFrame df_sales_amount๋ฅผ ๊ณ ๊ฐ ID๋ณ๋ก ๊ทธ๋ฃนํํ๊ณ , agg() ๋ฉ์๋๋ก ๊ฐ ๊ทธ๋ฃน์ ๊ธ์ก ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๋ค. ์์ฑ๋ DataFrame์ df_sales_amount์ ๋์ ํ๋ค.
reset_index() ๋ฉ์๋๋ก df_sales_amount์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ๊ณ , customer_id ์ปฌ๋ผ์ ์ผ๋ฐ ์ปฌ๋ผ์ผ๋ก ๋ณ๊ฒฝํ๋ค.
scikit-learn ์ ์ฒ๋ฆฌ ๋ชจ๋์ minmax_scale() ํจ์๋ฅผ df_sales_amount์ amount ์ปฌ๋ผ์ ์ ์ฉํ์ฌ 0์์ 1 ์ฌ์ด๋ก ๊ฐ์ ์ค์ผ์ผ๋งํ๋ค.
๋ฐ๋ผ์ ์ด ์ฝ๋์์๋ ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ , ํน์ ๊ณ ๊ฐ(ID๊ฐ 'Z'๋ก ์์ํ๋ ๊ณ ๊ฐ)์ ํ์ ํํฐ๋งํ๊ณ , ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก์ ์ต์-์ต๋ ์ค์ผ์ผ๋ง์ ์ฌ์ฉํ์ฌ 0๊ณผ 1 ์ฌ์ด๋ก ์ค์ผ์ผ๋งํฉ๋๋ค.
# ์ฝ๋ ์์ 2 (fit์ ํตํด ๋ค๋ฅธ ๋ฐ์ดํฐ์์๋ ๋์ผํ ์ต์๊ฐ, ์ต๋๊ฐ์ผ๋ก ํ์คํํ ์ ์์)
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")',
engine='python'). \
groupby('customer_id'). \
agg({'amount':'sum'}).reset_index()
scaler = preprocessing.MinMaxScaler()
scaler.fit(df_sales_amount[['amount']])
df_sales_amount['scale_amount'] = scaler.transform(df_sales_amount[['amount']])
df_sales_amount.head(10)
ย | customer_id | amount | scale_amount |
---|---|---|---|
0 | CS001113000004 | 1298 | 0.053354 |
1 | CS001114000005 | 626 | 0.024157 |
2 | CS001115000010 | 3044 | 0.129214 |
3 | CS001205000004 | 1988 | 0.083333 |
4 | CS001205000006 | 3337 | 0.141945 |
5 | CS001211000025 | 456 | 0.016771 |
6 | CS001212000027 | 448 | 0.016423 |
7 | CS001212000031 | 296 | 0.009819 |
8 | CS001212000046 | 228 | 0.006865 |
9 | CS001212000070 | 456 | 0.016771 |
ย
ํด์ค:
์ด ์ฝ๋๋ ์๋งค์ ์ ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก ๋ฐ์ดํฐ์ ๋ํ ๊ธฐ๋ฅ ์ค์ผ์ผ๋ง์ ์ํํ๋ค. ๋ค์์ ๊ฐ ํ์ ์ฒ๋ฆฌ ๋ด์ฉ์ด๋ค.
DF_SALES_AMOUNT: df_receipt ๋ฐ์ดํฐ ํ๋ ์์ customer_id๋ก ๊ทธ๋ฃนํํ๊ณ ๊ฐ ๊ทธ๋ฃน์ ๊ธ์ก์ ํฉ์ฐํ์ฌ ์ป์ customer_id์ ๊ฐ ๊ณ ๊ฐ์ด ์ฌ์ฉํ ์ด ๊ธ์ก์ ํฌํจํ๋ ์๋ก์ด DataFrame์ ์์ฑํ๋ค.
scaler: scikit-learn์ preprocessing ๋ชจ๋์์ MinMaxScaler ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๊ณ , ๋ฐ์ดํฐ๋ฅผ [0,1] ๋ฒ์์์ ์ค์ผ์ผ๋งํ๋ค.
scaler.fit(): df_sales_amount์ amount ์ด์ ์ค์ผ์ผ๋ฌ ๊ฐ์ฒด๋ฅผ ๋ง์ถ๊ณ ๋ฐ์ดํฐ์ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ๊ณ์ฐํ๋ค.
df_sales_amount['scale_amount']: df_sales_amount DataFrame์ ์๋ก์ด ์ปฌ๋ผ์ ์์ฑํ์ฌ ์ค์ผ์ผ๋ง๋ ๊ธ์ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค.
scaler.transform(): scaler ๊ฐ์ฒด๊ฐ ํ์ตํ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ์ฌ์ฉํ์ฌ ๊ฐ ๊ณ ๊ฐ์ ๊ธ์ก ๋ฐ์ดํฐ๋ฅผ ์ค์ผ์ผ๋งํ๋ค.
df_sales_amount.head(10): ์ ๋ฐ์ดํธ๋ df_sales_amount DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๊ณ ์๋ก์ด scale_amount ์ด์ ์ค์ผ์ผ๋ง๋ ๊ธ์ก ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๋ค.
์ ์ฒด์ ์ผ๋ก ์ด ์ฝ๋๋ ๊ฐ ๊ณ ๊ฐ์ ๊ธ์ก ๋ฐ์ดํฐ ๊ฐ์ ๋์ผํ ๋ฒ์๋ก ๋ง๋ค๊ธฐ ์ํด ํน์ง์ ์ธ ์ค์ผ์ผ๋ง์ ์ํํ์ฌ ๋น๊ต๊ฐ ๊ฐ๋ฅํ๊ณ ๋ถ์์ ๋์์ด ๋ ์ ์๋๋ก ํ๋ค.
ย
ย
Comment