๋ฐ์ดํฐ ์ฌ์ด์ธ์ค 100๋ฒ์ ๋ ธํฌ(๊ตฌ์กฐํ ๋ฐ์ดํฐ ์ฒ๋ฆฌํธ) โ Python Part 4 (Q61 to Q80)์ ํด์ค์ ๋๋ค.
ย
ย
์ฒ์์
- ๋จผ์ ๋ค์ ์ ์ ์คํํฉ๋๋ค.
- ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ ธ์ค๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ต๋๋ค(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-061: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก(amount)์ ๊ณ ๊ฐ ID(customer_id)๋ณ๋ก ํฉ์ฐํ๊ณ , ๋งค์ถ ๊ธ์ก ํฉ๊ณ๋ฅผ ์์ ๋์ํ(ํ๋จ 10)ํ์ฌ ๊ณ ๊ฐ ID, ๋งค์ถ ๊ธ์ก ํฉ๊ณ์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ค. ๋จ, ๊ณ ๊ฐ ID๊ฐ โZโ๋ก ์์ํ๋ ๊ฒ์ ๋นํ์์ ์๋ฏธํ๋ฏ๋ก ์ ์ธํ์ฌ ๊ณ์ฐํ๋ค.
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['log_amount'] = np.log10(df_sales_amount['amount'] + 0.5)
df_sales_amount.head(10)
ย | customer_id | amount | log_amount |
---|---|---|---|
0 | CS001113000004 | 1298 | 3.113442 |
1 | CS001114000005 | 626 | 2.796921 |
2 | CS001115000010 | 3044 | 3.483516 |
3 | CS001205000004 | 1988 | 3.298526 |
4 | CS001205000006 | 3337 | 3.523421 |
5 | CS001211000025 | 456 | 2.659441 |
6 | CS001212000027 | 448 | 2.651762 |
7 | CS001212000031 | 296 | 2.472025 |
8 | CS001212000046 | 228 | 2.358886 |
9 | CS001212000070 | 456 | 2.659441 |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ๋์์ ์ํํฉ๋๋ค.
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")', engine='python')
df_receipt๋ผ๋ DataFrame์์ customer_id ์ด์ด "Z"๋ก ์์ํ์ง ์๋ ํ์ ๋ชจ๋ ์ ํํ๋ค. ์์ฑ๋ DataFrame์ df_sales_amount๋ก ์ ์ฅ๋๋ค.
df_sales_amount = df_sales_amount.groupby('customer_id').agg({'amount':'sum'}).reset_index()
df_sales_amount์ DataFrame์ customer_id๋ก ๊ทธ๋ฃนํํ๊ณ , agg ๋ฉ์๋๋ก ๊ฐ ๊ทธ๋ฃน์ ๊ธ์ก ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํฉ๋๋ค. ๊ฒฐ๊ณผ DataFrame์ df_sales_amount๋ก ์ ์ฅ๋ฉ๋๋ค.
df_sales_amount['log_amount'] = np.log10(df_sales_amount['amount'] + 0.5)
df_sales_amount DataFrame์ amount ์ด์ ๋์(ํ๋จ 10)๋ฅผ ๊ณ์ฐํ๋๋ฐ, 0 ๋๋ ์์ ๊ฐ์ ๋์๋ฅผ ์ทจํ์ง ์๊ธฐ ์ํด ๋จผ์ amount ๊ฐ์ 0.5๋ฅผ ๋ํ๊ณ ์๋ค. ๊ฒฐ๊ณผ ๋์๋ log_amount๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅ๋ฉ๋๋ค.
df_sales_amount.head(10)
head ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ df_sales_amount DataFrame์ ์ฒ์ 10์ค์ ํ์ํฉ๋๋ค.
์์ฝํ๋ฉด, ์ด ์ฝ๋๋ ๊ณ ๊ฐ ID๊ฐ "Z"๋ก ์์ํ์ง ์๋ DataFrame์์ ํ์ ์ ํํ๊ณ , ๊ทธ ํ์ ๊ณ ๊ฐ ID๋ก ๊ทธ๋ฃนํํ๊ณ , ๊ฐ ๊ณ ๊ฐ์ ํ๋งค ๊ธ์ก์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ , ํ๋งค ๊ธ์ก์ ๋์(10์ ๊ธฐ์ค์ผ๋ก)๋ฅผ ์ทจํ์ฌ ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํฉ๋๋ค.
P-062: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ๊ธ์ก(amount)์ ๊ณ ๊ฐID(customer_id)๋ณ๋ก ํฉ์ฐํ๊ณ , ๋งค์ถ๊ธ์ก ํฉ๊ณ๋ฅผ ์์ฐ๋์ํ(ํ๋จ e)ํ์ฌ ๊ณ ๊ฐID, ๋งค์ถ๊ธ์ก ํฉ๊ณ์ ํจ๊ป 10๊ฑด์ฉ ํ์ํ๋ค. ๋จ, ๊ณ ๊ฐ ID๊ฐ โZโ๋ก ์์ํ๋ ๊ฒ์ ๋นํ์์ ์๋ฏธํ๋ฏ๋ก ์ ์ธํ์ฌ ๊ณ์ฐํ๋ค.
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['log_amount'] = np.log(df_sales_amount['amount'] + 0.5)
df_sales_amount.head(10)
ย | customer_id | amount | log_amount |
---|---|---|---|
0 | CS001113000004 | 1298 | 7.168965 |
1 | CS001114000005 | 626 | 6.440149 |
2 | CS001115000010 | 3044 | 8.021092 |
3 | CS001205000004 | 1988 | 7.595136 |
4 | CS001205000006 | 3337 | 8.112977 |
5 | CS001211000025 | 456 | 6.123589 |
6 | CS001212000027 | 448 | 6.105909 |
7 | CS001212000031 | 296 | 5.692047 |
8 | CS001212000046 | 228 | 5.431536 |
9 | CS001212000070 | 456 | 6.123589 |
ํด์ค:
์ด ์ฝ๋๋ df_receipt๋ผ๋ pandas์ DataFrame์ ๋ํด ๋ช ๊ฐ์ง ๋ฐ์ดํฐ ์ฒ๋ฆฌ๋ฅผ ์ํํฉ๋๋ค.
๋จผ์ customer_id ์ด์ด "Z"๋ก ์์ํ๋ ํ์ ํํฐ๋งํ๊ณ ์์ต๋๋ค. ์ด๋ str.startswith() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์กฐ๊ฑด๋ถ๋ก query() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ, engine='python' ์ธ์๋ ๋ฌธ์์ด ์กฐ์์ ๊ธฐ๋ณธ pandas ํ์๊ฐ ์๋ Python ํ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ์ ๊ณต๋์์ต๋๋ค.
์ป์ด์ง DataFrame์ groupby() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ customer_id ์ปฌ๋ผ์ผ๋ก ๊ทธ๋ฃนํ๋๋ค.
agg() ๋ฉ์๋๋ ๊ฐ ๊ทธ๋ฃน์ ๊ธ์ก ์ปฌ๋ผ์ ํจ์๋ฅผ ์ ์ฉํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ด ๊ฒฝ์ฐ sum() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ณ ๊ฐ์ ๋งค์ถ ๊ธ์ก์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํฉ๋๋ค.
๊ทธ๋ฐ ๋ค์ reset_index()๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ DataFrame์ ์ฌ์ค์ ํ๊ณ ์๋ก์ด ์ธ๋ฑ์ค๋ฅผ ๊ฐ๋๋ก ํฉ๋๋ค.
๋ง์ง๋ง์ผ๋ก, amount ์ด์ ์์ฐ๋์์ 0.5๋ฅผ ๋ํ ๊ฐ(0 ๋๋ ์์ ๊ฐ์ ๋์๋ฅผ ์ทจํ์ง ์๊ธฐ ์ํด)์ ์ทจํ์ฌ log_amount๋ผ๋ ์๋ก์ด ์ด์ ๋ง๋ญ๋๋ค.
๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ head() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ์ํฉ๋๋ค.
P-063: ์ํ ๋ฐ์ดํฐ(df_product)์ ๋จ๊ฐ(unit_price)์ ์๊ฐ(unit_cost)๋ก๋ถํฐ ๊ฐ ์ํ์ ์ด์ต์ก์ ์ฐ์ถํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ 10๊ฑด ํ์ํ์์ค.
df_tmp = df_product.copy()
df_tmp['unit_profit'] = df_tmp['unit_price'] - df_tmp['unit_cost']
df_tmp.head(10)
ย | product_cd | category_major_cd | category_medium_cd | category_small_cd | unit_price | unit_cost | unit_profit |
---|---|---|---|---|---|---|---|
0 | P040101001 | 04 | 0401 | 040101 | 198.0 | 149.0 | 49.0 |
1 | P040101002 | 04 | 0401 | 040101 | 218.0 | 164.0 | 54.0 |
2 | P040101003 | 04 | 0401 | 040101 | 230.0 | 173.0 | 57.0 |
3 | P040101004 | 04 | 0401 | 040101 | 248.0 | 186.0 | 62.0 |
4 | P040101005 | 04 | 0401 | 040101 | 268.0 | 201.0 | 67.0 |
5 | P040101006 | 04 | 0401 | 040101 | 298.0 | 224.0 | 74.0 |
6 | P040101007 | 04 | 0401 | 040101 | 338.0 | 254.0 | 84.0 |
7 | P040101008 | 04 | 0401 | 040101 | 420.0 | 315.0 | 105.0 |
8 | P040101009 | 04 | 0401 | 040101 | 498.0 | 374.0 | 124.0 |
9 | P040101010 | 04 | 0401 | 040101 | 580.0 | 435.0 | 145.0 |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ๋์์ ์ํํฉ๋๋ค.
df_tmp = df_product.copy()
df_product๋ผ๋ DataFrame์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ด df_tmp๋ผ๋ ์๋ก์ด ๋ณ์์ ๋์ ํ๊ณ ์์ต๋๋ค. ๋ณต์ฌ๋ณธ์ ์๋ณธ DataFrame์ ๋ณ๊ฒฝํ์ง ์๋๋ก ๋ง๋ค์ด์ก์ต๋๋ค.
df_tmp['unit_profit'] = df_tmp['unit_price'] - df_tmp['unit_cost'].
์ด๋ ๋จ๊ฐ์์ ๋จ๊ฐ๋ฅผ ๋นผ์ ๊ฐ ์ํ์ ๋จ์๋น ์ด์ต์ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ unit_profit์ด๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅํ๋ค. ์์ฑ๋ DataFrame์ df_tmp๋ก ์ ์ฅ๋ฉ๋๋ค.
df_tmp.head(10)
head ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ df_tmp DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๊ณ ์์ต๋๋ค.
์์ฝํ๋ฉด, ์ด ์ฝ๋๋ DataFrame์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค๊ณ , ๋จ๊ฐ์์ ๋จ๊ฐ๋ฅผ ๋นผ์ ๊ฐ ์ํ์ ๋จ์๋น ์ด์ต์ ๊ณ์ฐํ๊ณ , ๋ณต์ฌํ DataFrame์ ๋จ์๋น ์ด์ต์ด ์๋ ์๋ก์ด ์ด์ ์ถ๊ฐํ๊ณ , ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10์ค์ ํ์ํ๋ ๊ฒ์ ๋๋ค.
ย
P-064: ์ํ ๋ฐ์ดํฐ(df_product)์ ๋จ๊ฐ(unit_price)์ ์๊ฐ(unit_cost)์์ ๊ฐ ์ํ์ ์ ์ฒด ํ๊ท ์์ต๋ฅ ์ ๊ณ์ฐํ์์ค. ๋จ, ๋จ๊ฐ์ ์๊ฐ์๋ ๊ฒฐ์์ด ๋ฐ์ํ๋ค๋ ์ ์ ์ ์ํ๋ผ.
df_tmp = df_product.copy()
df_tmp['unit_profit_rate'] = \
(df_tmp['unit_price'] - df_tmp['unit_cost']) / df_tmp['unit_price']
df_tmp['unit_profit_rate'].mean(skipna=True)
0.24911389885176904
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ๋์์ ์ํํฉ๋๋ค.
df_tmp = df_product.copy()
df_product๋ผ๋ DataFrame์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ด df_tmp๋ผ๋ ์๋ก์ด ๋ณ์์ ๋์ ํ๊ณ ์์ต๋๋ค. ๋ณต์ฌ๋ณธ์ ์๋ณธ DataFrame์ ๋ณ๊ฒฝํ์ง ์๋๋ก ๋ง๋ค์ด์ก์ต๋๋ค.
df_tmp['unit_profit_rate'] = (df_tmp['unit_price'] - df_tmp['unit_cost']) / df_tmp['unit_price'].
์ด์ต(๋จ๊ฐ์์ ๋จ๊ฐ๋ฅผ ๋บ ๊ฒ)์ ๋จ๊ฐ๋ก ๋๋์ด ๊ฐ ์ํ์ ๋จ์๋น ์ด์ต๋ฅ ์ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ unit_profit_rate๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅํ๊ณ ์์ต๋๋ค. ์์ฑ๋ DataFrame์ df_tmp๋ก ์ ์ฅ๋ฉ๋๋ค.
df_tmp['unit_profit_rate'].mean(skipna=True)
df_tmp DataFrame์ unit_profit_rate ์ด์ ํ๊ท ๊ฐ์ mean ๋ฐฉ์์ผ๋ก ๊ณ์ฐํ๊ณ , ๊ฒฐ์๊ฐ(NaN)์ด ์์ผ๋ฉด ๊ฑด๋๋ฐ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ์ํ๋ค.
์์ฝํ๋ฉด, DataFrame์ ๋ณต์ฌ๋ณธ์ ์์ฑํ๊ณ , ์ด์ต(๋จ๊ฐ์์ ๋จ๊ฐ๋ฅผ ๋บ ๊ธ์ก)์ ๋จ๊ฐ๋ก ๋๋์ด ๊ฐ ์ํ์ ๋จ์๋น ์ด์ต๋ฅ ์ ๊ณ์ฐํ๊ณ , ๋ณต์ฌํ DataFrame์ ๋จ์๋น ์ด์ต๋ฅ ์ด์ ์๋ก ์ถ๊ฐํ๊ณ , ์ด์ต๋ฅ ์ด์ ํ๊ท ์ ๊ณ์ฐํ๊ณ , ๊ฒฐ๊ณผ์ ํ๊ท ๊ฐ์ ํ์ํ๋ ์ฝ๋์ ๋๋ค.
P-065: ์ํ ๋ฐ์ดํฐ(df_product)์ ๊ฐ ์ํ์ ๋ํด ์์ต๋ฅ ์ด 30%๊ฐ ๋๋ ์๋ก์ด ๋จ๊ฐ๋ฅผ ๊ตฌํ์์ค. ๋จ, 1์ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผํ๋ค. ๊ทธ๋ฆฌ๊ณ 10๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ํ๊ณ , ์์ต๋ฅ ์ด ๋๋ต 30% ์ ๋์ธ ๊ฒ์ ํ์ธํ๋ผ. ๋จ, ๋จ๊ฐ(unit_price)์ ์๊ฐ(unit_cost)์๋ ์ ์๊ฐ ๋ฐ์ํ๋ค๋ ์ ์ ์ ์ํ๋ผ.
df_tmp = df_product[['product_cd', 'unit_price', 'unit_cost']].copy()
df_tmp['new_price'] = np.floor(df_tmp['unit_cost'] / 0.7)
df_tmp['new_profit_rate'] = \
(df_tmp['new_price'] - df_tmp['unit_cost']) / df_tmp['new_price']
df_tmp.head(10)
ย | product_cd | unit_price | unit_cost | new_price | new_profit_rate |
---|---|---|---|---|---|
0 | P040101001 | 198.0 | 149.0 | 212.0 | 0.297170 |
1 | P040101002 | 218.0 | 164.0 | 234.0 | 0.299145 |
2 | P040101003 | 230.0 | 173.0 | 247.0 | 0.299595 |
3 | P040101004 | 248.0 | 186.0 | 265.0 | 0.298113 |
4 | P040101005 | 268.0 | 201.0 | 287.0 | 0.299652 |
5 | P040101006 | 298.0 | 224.0 | 320.0 | 0.300000 |
6 | P040101007 | 338.0 | 254.0 | 362.0 | 0.298343 |
7 | P040101008 | 420.0 | 315.0 | 450.0 | 0.300000 |
8 | P040101009 | 498.0 | 374.0 | 534.0 | 0.299625 |
9 | P040101010 | 580.0 | 435.0 | 621.0 | 0.299517 |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ๋์์ ์ํํฉ๋๋ค.
df_tmp = df_product[['product_cd', 'unit_price', 'unit_cost']].copy()
df_tmp๋ผ๋ ์๋ก์ด DataFrame์ ์์ฑํ์ฌ product_cd, unit_price, unit_cost๋ผ๋ ์ธ ๊ฐ์ ์ด๋ง ํฌํจํ๋๋ก ํ๊ณ , ์๋์ df_product DataFrame์์ ์ด ์ด๋ค์ ๋ณต์ฌํ๋ค.
df_tmp['new_price'] = np.floor(df_tmp['unit_cost'] / 0.7)
์ด๋ ๋จ๊ฐ๋ฅผ 0.7(๋งํฌ์ ๊ณ์ 1.43)๋ก ๋๋๊ณ floor ๋ฉ์๋๋ก ์์์ ์ดํ๋ฅผ ๋ฐ์ฌ๋ฆผํ์ฌ ๊ฐ ์ํ์ ์๋ก์ด ๊ฐ๊ฒฉ์ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ new_price๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅํ๊ณ ์์ต๋๋ค. ๊ฒฐ๊ณผ DataFrame์ df_tmp๋ก ์ ์ฅ๋๋ค.
df_tmp['new_profit_rate'] = (df_tmp['new_price'] - df_tmp['unit_cost']) / df_tmp['new_price'].
์ด์ ๋จ๊ณ์์ ๊ณ์ฐํ ์๋ก์ด ๊ฐ๊ฒฉ์ ์ฌ์ฉํ์ฌ ๊ฐ ์ํ์ ๋จ์๋น ์์ต๋ฅ ์ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ new_profit_rate๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅํฉ๋๋ค. ๊ฒฐ๊ณผ DataFrame์ df_tmp๋ก ์ ์ฅ๋ฉ๋๋ค.
df_tmp.head(10)
head ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ df_tmp DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๊ณ ์์ต๋๋ค.
์์ฝํ๋ฉด, ์ด ์ฝ๋๋ ์ธ ๊ฐ์ ์ด์ ๊ฐ์ง ์๋ก์ด DataFrame์ ๋ง๋ค๊ณ , ๋งํฌ์ ๊ณ์ 1.43์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ ์ํ์ ์๋ก์ด ๊ฐ๊ฒฉ์ ๊ณ์ฐํ๊ณ , ์๋ก์ด ๊ฐ๊ฒฉ์ ์ฌ์ฉํ์ฌ ๊ฐ ์ํ์ ๋จ์๋น ์์ต๋ฅ ์ ๊ณ์ฐํ๊ณ , ๋ณต์ฌํ DataFrame์ ์๋ก์ด ๋ ๊ฐ์ ์ด์ ์ถ๊ฐํ๊ณ , ๊ฒฐ๊ณผ์ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํฉ๋๋ค.
ย
ย
P-066: ์ํ ๋ฐ์ดํฐ(df_product)์ ๊ฐ ์ํ์ ๋ํด ์์ต๋ฅ ์ด 30%๊ฐ ๋๋ ์๋ก์ด ๋จ๊ฐ๋ฅผ ๊ตฌํ์์ค. ์ด๋ฒ์๋ 1์ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผํ๋ค(๋ฐ์ฌ๋ฆผ ๋๋ ์ง์๋ก ๋ฐ์ฌ๋ฆผํด๋ ๋ฌด๋ฐฉํ๋ค). ๊ทธ๋ฆฌ๊ณ 10๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ํ๊ฒ ํ๊ณ , ์์ต๋ฅ ์ด ๋๋ต 30% ์ ๋์ธ ๊ฒ์ ํ์ธํ๋ผ. ๋จ, ๋จ๊ฐ(unit_price)์ ์๊ฐ(unit_cost)์๋ ๊ฒฐ์์ด ๋ฐ์ํ๋ค๋ ์ ์ ์ ์ํ๋ค.
df_tmp = df_product[['product_cd', 'unit_price', 'unit_cost']].copy()
df_tmp['new_price'] = np.round(df_tmp['unit_cost'] / 0.7)
df_tmp['new_profit_rate'] = \
(df_tmp['new_price'] - df_tmp['unit_cost']) / df_tmp['new_price']
df_tmp.head(10)
ย | product_cd | unit_price | unit_cost | new_price | new_profit_rate |
---|---|---|---|---|---|
0 | P040101001 | 198.0 | 149.0 | 213.0 | 0.300469 |
1 | P040101002 | 218.0 | 164.0 | 234.0 | 0.299145 |
2 | P040101003 | 230.0 | 173.0 | 247.0 | 0.299595 |
3 | P040101004 | 248.0 | 186.0 | 266.0 | 0.300752 |
4 | P040101005 | 268.0 | 201.0 | 287.0 | 0.299652 |
5 | P040101006 | 298.0 | 224.0 | 320.0 | 0.300000 |
6 | P040101007 | 338.0 | 254.0 | 363.0 | 0.300275 |
7 | P040101008 | 420.0 | 315.0 | 450.0 | 0.300000 |
8 | P040101009 | 498.0 | 374.0 | 534.0 | 0.299625 |
9 | P040101010 | 580.0 | 435.0 | 621.0 | 0.299517 |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ๋์์ ์ํํฉ๋๋ค.
df_tmp = df_product[['product_cd', 'unit_price', 'unit_cost']].copy()
df_tmp๋ผ๋ ์๋ก์ด DataFrame์ ์์ฑํ์ฌ product_cd, unit_price, unit_cost ์ธ ๊ฐ์ ์ด๋ง ํฌํจํ๋๋ก ํ๊ณ , ์๋์ df_product DataFrame์์ ์ด ์ด๋ค์ ๋ณต์ฌํ๋ค.
df_tmp['new_price'] = np.round(df_tmp['unit_cost'] / 0.7)
์ด๊ฒ์ ๋จ๊ฐ๋ฅผ 0.7(๋งํฌ์ ๊ณ์ 1.43)๋ก ๋๋์ด ๊ฐ ์ํ์ ์๋ก์ด ๊ฐ๊ฒฉ์ ๊ณ์ฐํ๊ณ , round ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์๋ก ๋ฐ์ฌ๋ฆผํ์ฌ ๊ฒฐ๊ณผ๋ฅผ new_price๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅํ๊ณ ์์ต๋๋ค. ๊ฒฐ๊ณผ DataFrame์ df_tmp๋ก ์ ์ฅ๋ฉ๋๋ค.
df_tmp['new_profit_rate'] = (df_tmp['new_price'] - df_tmp['unit_cost']) / df_tmp['new_price'].
์ด์ ๋จ๊ณ์์ ๊ณ์ฐํ ์๋ก์ด ๊ฐ๊ฒฉ์ ์ฌ์ฉํ์ฌ ๊ฐ ์ํ์ ๋จ์๋น ์์ต๋ฅ ์ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ new_profit_rate๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์ ์ฅํฉ๋๋ค. ๊ฒฐ๊ณผ DataFrame์ df_tmp๋ก ์ ์ฅ๋ฉ๋๋ค.
df_tmp.head(10)
head ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ df_tmp DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๊ณ ์์ต๋๋ค.
์์ฝํ๋ฉด, ์ด ์ฝ๋๋ ์ธ ๊ฐ์ ์ด์ ๊ฐ์ง ์๋ก์ด DataFrame์ ์์ฑํ๊ณ , ๋งํฌ์ ๊ณ์ 1.43๊ณผ ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์๋ก ๋ฐ์ฌ๋ฆผํ์ฌ ๊ฐ ์ ํ์ ์๋ก์ด ๊ฐ๊ฒฉ์ ๊ณ์ฐํ๊ณ , ์๋ก์ด ๊ฐ๊ฒฉ์ ์ฌ์ฉํ์ฌ ๊ฐ ์ ํ์ ๋จ์๋น ์์ต๋ฅ ์ ๊ณ์ฐํ๊ณ , ๋ณต์ฌํ DataFrame์ ์๋ก์ด ๋ ๊ฐ์ ์ด์ ์ถ๊ฐํ๊ณ , ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๋ ๊ฒ์ ๋๋ค.
ย
P-067: ์ํ ๋ฐ์ดํฐ(df_product)์ ๊ฐ ์ํ์ ๋ํด ์์ต๋ฅ ์ด 30%๊ฐ ๋๋ ์๋ก์ด ๋จ๊ฐ๋ฅผ ๊ตฌํ์์ค. ์ด๋ฒ์๋ 1์ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผํ๋ค. ๊ทธ๋ฆฌ๊ณ 10๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ํ๊ณ , ์์ต๋ฅ ์ด ๋๋ต 30% ์ ๋์ธ ๊ฒ์ ํ์ธํ๋ผ. ๋จ, ๋จ๊ฐ(unit_price)์ ์๊ฐ(unit_cost)์๋ ์ ์๊ฐ ๋ฐ์ํ๊ณ ์๋ค๋ ์ ์ ์ ์ํ๋ผ.
df_tmp = df_product[['product_cd', 'unit_price', 'unit_cost']].copy()
df_tmp['new_price'] = np.ceil(df_tmp['unit_cost'] / 0.7)
df_tmp['new_profit_rate'] = \
(df_tmp['new_price'] - df_tmp['unit_cost']) / df_tmp['new_price']
df_tmp.head(10)
ย | product_cd | unit_price | unit_cost | new_price | new_profit_rate |
---|---|---|---|---|---|
0 | P040101001 | 198.0 | 149.0 | 213.0 | 0.300469 |
1 | P040101002 | 218.0 | 164.0 | 235.0 | 0.302128 |
2 | P040101003 | 230.0 | 173.0 | 248.0 | 0.302419 |
3 | P040101004 | 248.0 | 186.0 | 266.0 | 0.300752 |
4 | P040101005 | 268.0 | 201.0 | 288.0 | 0.302083 |
5 | P040101006 | 298.0 | 224.0 | 320.0 | 0.300000 |
6 | P040101007 | 338.0 | 254.0 | 363.0 | 0.300275 |
7 | P040101008 | 420.0 | 315.0 | 451.0 | 0.301552 |
8 | P040101009 | 498.0 | 374.0 | 535.0 | 0.300935 |
9 | P040101010 | 580.0 | 435.0 | 622.0 | 0.300643 |
ํด์ค:
์ด ์ฝ๋๋ df_product๋ผ๋ pandas DataFrame ๊ฐ์ฒด๋ฅผ ์กฐ์ํ๊ณ ์์ต๋๋ค.
์๋ ์ฝ๋๋ฅผ ํ ์ค์ฉ ๋ถํดํด ๋ณด๊ฒ ์ต๋๋ค.
df_tmp = df_product[['product_cd', 'unit_price', 'unit_cost']].copy(): ์ด ๋ผ์ธ์ df_product์ ํ์ ์งํฉ์ผ๋ก "product_cd", "unit_price", "unit_cost"์ ์ด๋ง ๋ฅผ ํฌํจํ๋ df_tmp๋ผ๋ ์๋ก์ด DataFrame ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. .copy() ๋ฉ์๋๋ ์๋ณธ df_product๋ฅผ ์ฐธ์กฐํ๋ ๋์ DataFrame์ ์๋ก์ด ๋ณต์ฌ๋ณธ์ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
df_tmp['new_price'] = np.ceiling(df_tmp['unit_cost'] / 0.7): ์ด ํ์ df_tmp์ 'new_price'๋ผ๋ ์๋ก์ด ์ด์ ๊ณ์ฐํ๋ค. ์ด๋ 'unit_cost' ์ด์ 0.7๋ก ๋๋ ํ numpy์ ceiling() ํจ์๋ก ์ํ์ ๊ตฌํ ๊ฒฐ๊ณผ์ ๋๋ค. ๊ทธ๋ฌ๋ฉด ์ด์ค์จ 30%๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋ก์ด ๊ฐ๊ฒฉ์ด ๊ณ์ฐ๋ฉ๋๋ค.
df_tmp['new_profit_rate'] = \ (df_tmp['new_price'] - df_tmp['unit_cost']) / df_tmp['new_price']: ์ด ํ์ df_tmp์ 'new_profit_rate'๋ผ๋ ์๋ก์ด ์ด์ ๋ง๋ค๊ณ 'new_price' ์ด์ 'unit_cost'๋ฅผ ์ ๋ ฅํ๋ค. new_price' ์ด์์ 'unit_cost' ์ด์ ๋นผ๊ณ 'new_price' ์ด๋ก ๋๋ ๊ฒฐ๊ณผ์ด๋ค. ์ด๋ฅผ ํตํด ์๋ก์ด ๊ฐ๊ฒฉ์ ๋ํ ์์ต๋ฅ ์ด ๊ณ์ฐ๋๋ค.
df_tmp.head(10): df_tmp DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ์ถ๋ ฅํ๋ค. ์ด ํ์ ์ด์ ํ์ ๊ณ์ฐ์ด ์ ๋๋ก ์ด๋ฃจ์ด์ก๋์ง ํ์ธํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
P-068: ์ํ ๋ฐ์ดํฐ(df_product)์ ๊ฐ ์ํ์ ๋ํด ์๋น์ธ์จ 10%์ ๋ถ๊ฐ์ธ ํฌํจ ๊ธ์ก์ ๊ตฌํ๊ณ , 1์ ๋ฏธ๋ง์ ๋จ์๋ ์ ์ฌํ์ฌ 10๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ํ์์ค. ๋จ, ๋จ๊ฐ(unit_price)์๋ ๊ฒฐ์์ด ๋ฐ์ํ๋ค๋ ์ ์ ์ ์ํ๋ผ.
df_tmp = df_tmp = df_product[['product_cd', 'unit_price']].copy()
df_tmp['tax_price'] = np.floor(df_tmp['unit_price'] * 1.1)
df_tmp.head(10)
ย | product_cd | unit_price | tax_price |
---|---|---|---|
0 | P040101001 | 198.0 | 217.0 |
1 | P040101002 | 218.0 | 239.0 |
2 | P040101003 | 230.0 | 253.0 |
3 | P040101004 | 248.0 | 272.0 |
4 | P040101005 | 268.0 | 294.0 |
5 | P040101006 | 298.0 | 327.0 |
6 | P040101007 | 338.0 | 371.0 |
7 | P040101008 | 420.0 | 462.0 |
8 | P040101009 | 498.0 | 547.0 |
9 | P040101010 | 580.0 | 638.0 |
ํด์ค:
์ด ์ฝ๋์์๋ df_product๋ผ๋ ์ด๋ฆ์ pandas DataFrame ๊ฐ์ฒด๋ ์กฐ์ํ๊ณ ์์ต๋๋ค.
์๋ ์ฝ๋๋ฅผ ํ ์ค์ฉ ๋ถํดํด ๋ณด๊ฒ ์ต๋๋ค.
df_tmp = df_product[['product_cd', 'unit_price']].copy(): ์ด ์ค์ df_product์ ํ์ ์งํฉ์ผ๋ก "product_cd"์ "unit_price" ์ปฌ๋ผ๋ง ํฌํจํ๋ df_tmp๋ผ๋ ์๋ก์ด DataFrame ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค. .copy() ๋ฉ์๋๋ ์๋์ df_product๋ฅผ ์ฐธ์กฐํ๋ ๋์ DataFrame์ ์๋ก์ด ๋ณต์ฌ๋ณธ์ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
df_tmp['tax_price'] = np.floor(df_tmp['unit_price'] * 1.1): ์ด ํ์ df_tmp์ 'tax_price'๋ผ๋ ์๋ก์ด ์ด์ ๊ณ์ฐํ๋ค. ์ด ์ด์ 'unit_price' ์ด์ 1.1์ ๊ณฑํ๊ณ ๊ทธ ๊ฒฐ๊ณผ์ ๋ฐ๋ฅ์ numpy์ floor() ํจ์๋ก ๊ตฌํ ๊ฒฐ๊ณผ์ ๋๋ค. ๊ทธ๋ฌ๋ฉด 10%์ ์ธ๊ธ์ด ์ถ๊ฐ๋ ์๋ก์ด ๊ฐ๊ฒฉ์ด ๊ณ์ฐ๋๋ค.
df_tmp.head(10): ์ด ํ์ df_tmp์ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ์ถ๋ ฅํ๋ค. ์ด์ ํ์ ๊ณ์ฐ์ด ์ ๋๋ก ์ด๋ฃจ์ด์ก๋์ง ํ์ธํ๋ ๋ฐ ์ฌ์ฉํฉ๋๋ค.
ย
P-069: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ์ํ ๋ฐ์ดํฐ(df_product)๋ฅผ ๊ฒฐํฉํ์ฌ ๊ณ ๊ฐ๋ณ ์ ์ฒด ์ํ ํ๋งค๊ธ์ก ํฉ๊ณ์ ์นดํ ๊ณ ๋ฆฌ ๋๋ถ๋ฅ ์ฝ๋(category_major_cd)๊ฐ โ07โ(๋ณ์กฐ๋ฆผ ํต์กฐ๋ฆผ)์ธ ์ํ์ ํ๋งค๊ธ์ก ํฉ๊ณ๋ฅผ ๊ณ์ฐํ ํ, ์์์ ๋น์จ์ ๊ตฌํ์์ค. ์ถ์ถ ๋์์ ์นดํ ๊ณ ๋ฆฌ ๋๋ถ๋ฅ ์ฝ๋ โ07โ(๋ณ์กฐ๋ฆผ ํต์กฐ๋ฆผ)์ ํ๋งค ์ค์ ์ด ์๋ ๊ณ ๊ฐ์ผ๋ก ํ์ ํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ 10๊ฑด๋ง ํ์ํ๋ค.
# ์ฝ๋ ์์ 1
df_tmp_1 = df_receipt.groupby('customer_id').agg({'amount':'sum'}). \
reset_index().rename(columns={'amount':'sum_all'})
df_tmp_2 = pd.merge(df_receipt, df_product.query('category_major_cd == "07"'),
how='inner', on='product_cd').groupby('customer_id').\
agg({'amount':'sum'}).reset_index().\
rename(columns={'amount':'sum_07'})
df_tmp_3 = pd.merge(df_tmp_1, df_tmp_2, how='inner', on='customer_id')
df_tmp_3['sales_rate'] = df_tmp_3['sum_07'] / df_tmp_3['sum_all']
df_tmp_3.head(10)
ย | customer_id | sum_all | sum_07 | sales_rate |
---|---|---|---|---|
0 | CS001113000004 | 1298 | 1298 | 1.000000 |
1 | CS001114000005 | 626 | 486 | 0.776358 |
2 | CS001115000010 | 3044 | 2694 | 0.885020 |
3 | CS001205000004 | 1988 | 346 | 0.174044 |
4 | CS001205000006 | 3337 | 2004 | 0.600539 |
5 | CS001212000027 | 448 | 200 | 0.446429 |
6 | CS001212000031 | 296 | 296 | 1.000000 |
7 | CS001212000046 | 228 | 108 | 0.473684 |
8 | CS001212000070 | 456 | 308 | 0.675439 |
9 | CS001213000018 | 243 | 145 | 0.596708 |
ํด์ค:
์ด ์ฝ๋์์๋ pandas์ DataFrame ๊ฐ์ฒด์ธ df_receipt์ df_product๋ ์กฐ์ํ๊ณ ์์ต๋๋ค.
์๋ ์ฝ๋๋ฅผ ํ ์ค์ฉ ๋ถํดํด ๋ณด๊ฒ ์ต๋๋ค.
df_tmp_1 = df_receipt.groupby('customer_id').agg({'amount':'sum'}).reset_index().rename(columns={'amount':'sum_all'}): ์ด ์ค์ df_receipt DataFrame์ "customer_id"๋ก ๋ณ๊ฒฝํฉ๋๋ค. receipt DataFrame์ "customer_id"๋ก ๊ทธ๋ฃนํํ์ฌ ๊ทธ๋ฃน๋ณ๋ก "amount" ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ DataFrame์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ๊ณ ์์ต๋๋ค. ์ดํ .rename() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ DataFrame์ ์ด๋ฆ์ "sum_all"๋ก ๋ณ๊ฒฝํฉ๋๋ค.
df_tmp_2 = pd.merge(df_receipt, df_product.query('category_major_cd == "07"'), how='inner', on='product_cd').groupby('customer_id').agg ({'amount': sum'}).reset_index().rename(columns={'amount': 'sum_07' }): ์ด ๋ผ์ธ์ ๋จผ์ df_product๋ฅผ ํํฐ๋งํ์ฌ category_major_cd ์ด์ด "07"๊ณผ ๊ฐ์ ํ๋ง ํฌํจํ๋๋ก ํ๋๋ก ํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ์ด ํํฐ๋ง๋ DataFrame์ ๋ด๋ถ ๋ฐ์ธ๋ฉ์ ์ฌ์ฉํ์ฌ "product_cd" ์ด์ df_receipt์ ๊ฒฐํฉํ๋ค. ๊ทธ๋ฐ ๋ค์ ๊ฒฐ๊ณผ DataFrame์ 'customer_id'๋ก ๊ทธ๋ฃนํํ์ฌ ๊ทธ๋ฃน๋ณ๋ก 'amount' ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ๋ค. ์์ฑ๋ DataFrame์ .rename() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ "sum_07"๋ก ์ด๋ฆ์ด ๋ณ๊ฒฝ๋๋ค.
df_tmp_3 = pd.merge(df_tmp_1, df_tmp_2, how='inner', on='customer_id'): ์ด ํ์ ๋ด๋ถ ๊ฒฐํฉ์ ์ฌ์ฉํ์ฌ df_tmp_1๊ณผ df_tmp_2๋ฅผ 'customer_id' ์ปฌ๋ผ์ผ๋ก ๋ณํฉํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๊ฐ ๊ณ ๊ฐ์ 'sum_all' ์ปฌ๋ผ๊ณผ 'sum_07' ์ปฌ๋ผ์ ํฌํจํ๋ ์๋ก์ด DataFrame์ด ์์ฑ๋๋ค.
df_tmp_3['sales_rate'] = df_tmp_3['sum_07'] / df_tmp_3['sum_all']: ์ด ํ์ df_tmp_3์ ์๋ก์ด ์ด 'sales_rate'๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ผ๋ก, 'sum_07' ์ด์ 'sum_all' ์ด๋ก ๋๋ ๊ฒฐ๊ณผ์ ๋๋ค. ์ด๋ ๊ณ ๊ฐ์ ์ด ๊ตฌ๋งค์ก ์ค '07' ์นดํ ๊ณ ๋ฆฌ์์ ๊ตฌ๋งคํ ๊ธ์ก์ ๋น์จ์ ๊ณ์ฐํ๋ ๊ฒ์ด๋ค.
df_tmp_3.head(10): df_tmp_3 DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ์ถ๋ ฅํ๋ค. ์ด ํ์ ์ด์ ํ์ ๊ณ์ฐ์ด ์ ๋๋ก ์ด๋ฃจ์ด์ก๋์ง ํ์ธํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
ย
# ์ฝ๋ ์์ 2 (์ฐธ๊ณ , unstack๊ณผ ํก๋ฐฉํฅ sum์ ์ฌ์ฉํ ์์)
df_temp = df_receipt.merge(df_product, how='left', on='product_cd'). \
groupby(['customer_id', 'category_major_cd'])['amount'].sum().unstack()
df_temp = df_temp[df_temp['07'] > 0]
df_temp['sum_all'] = df_temp.sum(axis=1)
df_temp['sales_rate'] = df_temp['07'] / df_temp['sum_all']
# ์ดํ๋ ๋ฐ์ดํฐ ํ๋ ์์ ์ ํํ ๋ฐ ํ์๋ฅผ ์ํ ์ฒ๋ฆฌ
df_temp.columns.name = ''
df_temp = df_temp.reset_index()
df_temp.head(10)
ย | customer_id | 04 | 05 | 06 | 07 | 08 | 09 | sum_all | sales_rate |
---|---|---|---|---|---|---|---|---|---|
0 | CS001113000004 | NaN | NaN | NaN | 1298.0 | NaN | NaN | 1298.0 | 1.000000 |
1 | CS001114000005 | NaN | 40.0 | NaN | 486.0 | 100.0 | NaN | 626.0 | 0.776358 |
2 | CS001115000010 | NaN | NaN | NaN | 2694.0 | NaN | 350.0 | 3044.0 | 0.885020 |
3 | CS001205000004 | 100.0 | 128.0 | 286.0 | 346.0 | 368.0 | 760.0 | 1988.0 | 0.174044 |
4 | CS001205000006 | 635.0 | 60.0 | 198.0 | 2004.0 | 80.0 | 360.0 | 3337.0 | 0.600539 |
5 | CS001212000027 | 248.0 | NaN | NaN | 200.0 | NaN | NaN | 448.0 | 0.446429 |
6 | CS001212000031 | NaN | NaN | NaN | 296.0 | NaN | NaN | 296.0 | 1.000000 |
7 | CS001212000046 | NaN | NaN | NaN | 108.0 | NaN | 120.0 | 228.0 | 0.473684 |
8 | CS001212000070 | NaN | NaN | 148.0 | 308.0 | NaN | NaN | 456.0 | 0.675439 |
9 | CS001213000018 | NaN | NaN | NaN | 145.0 | 98.0 | NaN | 243.0 | 0.596708 |
ํด์ค:
์ด ์ฝ๋์์๋ pandas์ DataFrame ๊ฐ์ฒด์ธ df_receipt์ df_product๋ ์กฐ์ํ๊ณ ์์ต๋๋ค.
์๋ ์ฝ๋๋ฅผ ํ ์ค์ฉ ๋ถํดํด ๋ณด๊ฒ ์ต๋๋ค.
df_temp = df_receipt.merge(df_product, how='left', on='product_cd').groupby(['customer_id', 'category_major_cd']) ['amount'].sum(). unstack(): ์ด ํ์ ๋จผ์ ์ผ์ชฝ ๊ฒฐํฉ์ ์ฌ์ฉํ์ฌ df_receipt์ df_product๋ฅผ "product_cd" ์ปฌ๋ผ์ผ๋ก ๊ฒฐํฉํ๋ค. ๊ทธ๋ฐ ๋ค์ ๊ฒฐ๊ณผ DataFrame์ "customer_id"์ "category_major_cd"๋ก ๊ทธ๋ฃนํํ์ฌ ๊ฐ ๊ทธ๋ฃน์ "amount" ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ , ๊ฒฐ๊ณผ ์๋ฆฌ์ฆ๋ฅผ ์ธ์คํํ์ฌ "customer_id"๋ฅผ ์ธ๋ฑ์ค๋ก, "category_major_cd"๋ฅผ ์ด ์ด๋ฆ์ผ๋ก ์ง์ ํฉ๋๋ค. category_major_cd", "amount" ์ด์ ๊ฐ์ผ๋ก ํ๋ DataFrame์ ๋ง๋ค์์ต๋๋ค.
df_temp = df_temp[df_temp['07'] > 0]: ์ด ํ์ df_temp DataFrame์ ํํฐ๋งํ์ฌ '07' ์ด์ ๊ฐ์ด 0๋ณด๋ค ํฐ ํ๋ง ํฌํจํ๋๋ก ํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด '07' ์นดํ ๊ณ ๋ฆฌ์์ ์๋ฌด๊ฒ๋ ๊ตฌ๋งคํ์ง ์์ ๊ณ ๊ฐ์ด ์ ๊ฑฐ๋ฉ๋๋ค.
df_temp['sum_all'] = df_temp.sum(axis=1): ์ด ํ์ df_temp์ 'sum_all'์ด๋ผ๋ ์๋ก์ด ์ด์ ๊ณ์ฐํ์ฌ ๊ฐ ํ์ ๋ชจ๋ ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํฉ๋๋ค. ์ด๋ ๊ฐ ๊ณ ๊ฐ์ด ๋ชจ๋ ์นดํ ๊ณ ๋ฆฌ์์ ์ง์ถํ ๊ธ์ก์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ด๋ค.
df_temp['sales_rate'] = df_temp['07'] / df_temp['sum_all']: ์ด ํ์ df_temp์ 'sales_rate'๋ผ๋ ์๋ก์ด ์ด์ ๊ณ์ฐํ๋ค. ์ด๋ ๊ณ ๊ฐ์ ์ด ๊ตฌ๋งค ๊ธ์ก ์ค '07' ์นดํ ๊ณ ๋ฆฌ์์ ๊ตฌ๋งคํ ๊ธ์ก์ ๋น์จ์ ๊ณ์ฐํ๋ ๊ฒ์ ๋๋ค.
df_temp.columns.name = '': ์ด ํ์ df_temp DataFrame์์ ์ด ์ด๋ฆ์ ์ญ์ ํ๊ณ ์์ต๋๋ค.
df_temp = df_temp.reset_index(): ์ด ํ์ df_temp์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ค์ ํ๊ณ 0๋ถํฐ ์์ํ๋ ์ ์ ๋ฒ์๋ฅผ ๊ฐ์ง 'index'๋ผ๋ ์๋ก์ด ์ด์ ์์ฑํฉ๋๋ค.
df_temp.head(10): df_temp ๋ฐ์ดํฐ ํ๋ ์์ ์ฒ์ 10๊ฐ์ ํ์ ์ถ๋ ฅํ๋ค. ์ด ํ์ ์ด์ ํ์ ๊ณ์ฐ์ด ์ฌ๋ฐ๋ฅด๊ฒ ์ํ๋์๋์ง ํ์ธํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
ย
P-070: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ์ผ(sales_ymd)์ ๋ํด ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ํ์๊ฐ์ ์ผ(application_date)๋ก๋ถํฐ์ ๊ฒฝ๊ณผ์ผ์๋ฅผ ๊ณ์ฐํ์ฌ ๊ณ ๊ฐ ID(customer_id), ๋งค์ถ์ผ, ํ์๊ฐ์ ์ผ๊ณผ ํจ๊ป 10๊ฑด์ ํ์ํ๋ผ(sales_ymd๋ ์์น, application_date๋ ๋ฌธ์์ด๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๊ณ ์๋ค๋ ์ ์ ์ ์).
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates()
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']],
how='inner', on='customer_id')
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str'))
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date'])
df_tmp['elapsed_days'] = df_tmp['sales_ymd'] - df_tmp['application_date']
df_tmp['elapsed_days'] = df_tmp['elapsed_days'].dt.days
df_tmp.head(10)
ย | customer_id | sales_ymd | application_date | elapsed_days |
---|---|---|---|---|
0 | CS006214000001 | 2018-11-03 | 2015-02-01 | 1371 |
1 | CS006214000001 | 2017-05-09 | 2015-02-01 | 828 |
2 | CS006214000001 | 2017-06-08 | 2015-02-01 | 858 |
3 | CS006214000001 | 2018-10-28 | 2015-02-01 | 1365 |
4 | CS006214000001 | 2019-09-08 | 2015-02-01 | 1680 |
5 | CS006214000001 | 2018-01-31 | 2015-02-01 | 1095 |
6 | CS006214000001 | 2017-07-05 | 2015-02-01 | 885 |
7 | CS006214000001 | 2018-11-10 | 2015-02-01 | 1378 |
8 | CS006214000001 | 2019-04-10 | 2015-02-01 | 1529 |
9 | CS006214000001 | 2019-06-01 | 2015-02-01 | 1581 |
ํด์ค:
์ด ์ฝ๋๋ df_receipt๋ผ๋ DataFrame๊ณผ df_customer๋ผ๋ ๋ ๋ค๋ฅธ DataFrame์ ๋ํด ๋ฐ์ดํฐ ์กฐ์์ ํ๊ณ ์์ต๋๋ค. ๊ณ ๊ฐ์ ์ ์ฒญ์ผ๊ณผ ๊ตฌ๋งค์ผ ์ฌ์ด์ ์ผ์๋ฅผ ๊ณ์ฐํ๊ณ ์์ต๋๋ค.
๋ค์์ ์ฝ๋์ ๋จ๊ณ๋ณ ์์์ ๋๋ค.
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates(): df_receipt DataFrame์์ "customer_id"์ "sales_ymd" ์ปฌ๋ผ๋ง ์ ํํด์ df_tmp๋ผ๋ ์๋ก์ด DataFrame์ ์์ฑํ๊ณ ์ค๋ณต๋ ํ์ ์ญ์ ํฉ๋๋ค. ๊ทธ ๊ฒฐ๊ณผ, ๊ณ ๊ฐ ID์ ๋งค์ถ ๋ ์ง์ ์กฐํฉ์ด ๊ณ ์ ํ DataFrame์ด ๋ง๋ค์ด์ก๋ค.
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']], how='inner', on='customer_id'): df_tmp์ df_customer DataFrame์ ๋ด๋ถ ๊ฒฐํฉ์ ์ํํ๊ณ , df_customer DataFrame์์ 'customer_id' ์ด๊ณผ 'application_date' ์ด๋ง ์ ํํ๋ค. ๊ฒฐ๊ณผ DataFrame์๋ df_tmp์ df_customer์ ๋ชจ๋ ์กด์ฌํ๋ ๊ณ ๊ฐ ID๋ฅผ ๊ฐ์ง ํ๋ง ํฌํจํ๊ฒ ๋๋ค.
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str')): df_tmp์ "sales_ymd" ์ด์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค. ์ด ํจ์๋ ์ปฌ๋ผ์ ๋ฌธ์์ด ํ์์์ ๊ณ์ฐ์ ์ฌ์ฉํ ์ ์๋ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date']): df_tmp์ "application_date" ์นผ๋ผ์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค. ๋ก ๋ณํํ๋ค. ์ด ํจ์๋ ์ปฌ๋ผ์ ๋ฌธ์์ด ํ์์์ ๊ณ์ฐ์ ์ฌ์ฉํ ์ ์๋ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['elapsed_days'] = df_tmp['sales_ymd'] - df_tmp['application_date']: "sales_ymd"์ "application_date" ์ด์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ df_tmp์ ์๋ก์ด ์ด "elapsed _days"์ ๋์ ํ๋ค. ์ด ๊ณ์ฐ์ ๊ฒฐ๊ณผ๋ก ๋ ๋ ์ง์ ์ฐจ์ด๋ฅผ ์ผ, ์๊ฐ, ๋ถ, ์ด ๋จ์๋ก ํํํ timedelta ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค.
df_tmp['elapsed_days'] = df_tmp['elapsed_days'].dt.days: "elapsed_days" ์ด์์ ์ผ์๋ง ๊ฐ์ ธ์์ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ์ด์ ๋์ ํ์ฌ ๋ฐํํ๋ค. ๊ฐ์ฒด๊ฐ ํฌํจ๋์ด ์์ง๋ง, ์ผ์๋ง ๋ณด๊ดํ๊ณ ์ถ๊ธฐ ๋๋ฌธ์ ์ด ๋จ๊ณ๊ฐ ํ์ํ๋ค.
df_tmp.head(10): df_tmp์ ์ฒ์ 10์ค์ ํ์ํ์ฌ ๊ณ์ฐ์ด ์ ๋๋ก ์ด๋ฃจ์ด์ก๋์ง ํ์ธํ๋ค.
์ ๋ฐ์ ์ผ๋ก ์ด ์ฝ๋๋ ๊ณ ๊ฐ์ ํ๋์ ๋ถ์ํ๊ณ ๊ณ ๊ฐ์ด ๋ฉค๋ฒ์ญ์ด๋ ๊ณ์ ์ ์ ์ฒญํ ํ ๊ตฌ๋งคํ๊ธฐ๊น์ง ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ํ์ ํ๋ ๋ฐ ์ ์ฉํ๋ค.
P-071: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ์ผ(sales_ymd)์ ๋ํด ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ํ์๊ฐ์ ์ผ(application_date)๋ก๋ถํฐ์ ๊ฒฝ๊ณผ ๊ฐ์ ์๋ฅผ ๊ณ์ฐํ์ฌ ๊ณ ๊ฐ ID(customer_id), ๋งค์ถ์ผ, ํ์๊ฐ์ ์ผ๊ณผ ํจ๊ป 10๊ฑด ํ์ (sales_ymd๋ ์ซ์, application_date๋ ๋ฌธ์์ด๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๋ ์ ์ ์ ์) 1๊ฐ์ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผํ๋ค.
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates()
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']],
how='inner', on='customer_id')
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str'))
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date'])
df_tmp['elapsed_months'] = df_tmp[['sales_ymd', 'application_date']]. \
apply(lambda x: relativedelta(x[0], x[1]).years * 12 + \
relativedelta(x[0], x[1]).months, axis=1)
df_tmp.head(10)
ย | customer_id | sales_ymd | application_date | elapsed_months |
---|---|---|---|---|
0 | CS006214000001 | 2018-11-03 | 2015-02-01 | 45 |
1 | CS006214000001 | 2017-05-09 | 2015-02-01 | 27 |
2 | CS006214000001 | 2017-06-08 | 2015-02-01 | 28 |
3 | CS006214000001 | 2018-10-28 | 2015-02-01 | 44 |
4 | CS006214000001 | 2019-09-08 | 2015-02-01 | 55 |
5 | CS006214000001 | 2018-01-31 | 2015-02-01 | 35 |
6 | CS006214000001 | 2017-07-05 | 2015-02-01 | 29 |
7 | CS006214000001 | 2018-11-10 | 2015-02-01 | 45 |
8 | CS006214000001 | 2019-04-10 | 2015-02-01 | 50 |
9 | CS006214000001 | 2019-06-01 | 2015-02-01 | 52 |
ํด์ค:
์ด ์ฝ๋๋ ์ง๋๋ฒ ์ค๋ช ํ ์ฝ๋์ ๋น์ทํ์ง๋ง, ๊ณ ๊ฐ์ ์ ์ฒญ์ผ๋ก๋ถํฐ ๊ตฌ๋งค์ผ๊น์ง์ ์ผ์๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ด ์๋๋ผ ๊ฒฝ๊ณผํ ๊ฐ์ ์๋ฅผ ๊ณ์ฐํ๋ ์ฝ๋์ ๋๋ค.
๋ค์์ ์ฝ๋๋ฅผ ๋จ๊ณ๋ณ๋ก ์ค๋ช ํฉ๋๋ค.
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates(): df_receipt DataFrame์์ customer_id์ sales_ymd ์ด๋ง ์ ํํ๊ณ ์ค๋ณต๋ ํ์ ์ญ์ ํ์ฌ ์๋ก์ด df_tmp๋ผ๋ DataFrame์ ์์ฑํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๊ณ ๊ฐ ID์ ํ๋งค ๋ ์ง์ ์กฐํฉ์ด ๊ณ ์ ํ DataFrame์ด ๋ง๋ค์ด์ง๋๋ค.
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']], how='inner', on='customer_id'): df_tmp์ df_customer DataFrame์ ๋ด๋ถ ๊ฒฐํฉ์ ์ํํ๊ณ , df_customer DataFrame์์ 'customer_id' ์ด๊ณผ 'application_date' ์ด๋ง ์ ํํ๋ค. ๊ฒฐ๊ณผ DataFrame์๋ df_tmp์ df_customer์ ๋ชจ๋ ์กด์ฌํ๋ ๊ณ ๊ฐ ID๋ฅผ ๊ฐ์ง ํ๋ง ํฌํจํ๊ฒ ๋๋ค.
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str')): df_tmp์ "sales_ymd" ์ด์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date']): df_tmp์ "application_date" ์ปฌ๋ผ์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['elapsed_months'] = df_tmp[['sales_ymd', 'application_date']]. \ ฮ apply(lambda x: relativedelta(x[0], x[1]).years * 12 + ฮ relativedelta(x[0], x[1]).months, axis=1): "sales_ymd"์ "application_date" ์ด์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํ๋ค, ๊ฒฐ๊ณผ๋ฅผ df_tmp์ "elapsed_months"๋ผ๋ ์๋ก์ด ์ด์ ๋์ ํ๋ค. ์ด ๊ณ์ฐ์ dateutil ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ relativedelta ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ datetime ๊ฐ์ ์ฐจ์ด๋ฅผ ๋ ๋๋ ์ ๋จ์๋ก ๊ณ์ฐํ๊ณ , apply() ๋ฉ์๋์์ "sales_ymd"์ "application_date" ์ด์ ๊ฐ ํ์ ์ด ํจ์๋ฅผ ์ ์ฉํ๊ณ ๊ฒฝ๊ณผํ ๊ฐ์์ ํฉ๊ณ๋ฅผ ๋ฐํํ๋ค.
df_tmp.head(10): df_tmp์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ์ฌ ๊ณ์ฐ์ด ์ ๋๋ก ์ด๋ฃจ์ด์ก๋์ง ํ์ธํ๋ค.
์ ์ฒด์ ์ผ๋ก ์ด ์ฝ๋๋ ๊ณ ๊ฐ์ ํ๋์ ๋ถ์ํ๊ณ ๊ณ ๊ฐ์ด ํ์ ๊ฐ์ ๋๋ ๊ณ์ ์ ์ ์ฒญํ ํ ๊ตฌ๋งคํ๊ธฐ๊น์ง์ ๊ธฐ๊ฐ์ ๊ฒฝ๊ณผ ๊ฐ์ ์๋ก ํ๋จํ๋ ๋ฐ ํธ๋ฆฌํฉ๋๋ค.
ย
P-072: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ์ผ(df_customer)์ ๋ํด ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ํ์๊ฐ์ ์ ์ฒญ์ผ(application_date)๋ก๋ถํฐ์ ๊ฒฝ๊ณผ๋ ์๋ฅผ ๊ณ์ฐํ์ฌ ๊ณ ๊ฐ ID(customer_id), ๋งค์ถ์ผ, ํ์๊ฐ์ ์ ์ฒญ์ผ๊ณผ ํจ๊ป 10๊ฑด (sales_ymd๋ ์์น, application_date๋ ๋ฌธ์์ด๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๊ณ ์๋ค๋ ์ ์ ์ ์) 1๋ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผํ๋ค.
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates()
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']],
how='inner', on='customer_id')
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str'))
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date'])
df_tmp['elapsed_years'] = df_tmp[['sales_ymd', 'application_date']]. \
apply(lambda x: relativedelta(x[0], x[1]).years, axis=1)
df_tmp.head(10)
ย | customer_id | sales_ymd | application_date | elapsed_years |
---|---|---|---|---|
0 | CS006214000001 | 2018-11-03 | 2015-02-01 | 3 |
1 | CS006214000001 | 2017-05-09 | 2015-02-01 | 2 |
2 | CS006214000001 | 2017-06-08 | 2015-02-01 | 2 |
3 | CS006214000001 | 2018-10-28 | 2015-02-01 | 3 |
4 | CS006214000001 | 2019-09-08 | 2015-02-01 | 4 |
5 | CS006214000001 | 2018-01-31 | 2015-02-01 | 2 |
6 | CS006214000001 | 2017-07-05 | 2015-02-01 | 2 |
7 | CS006214000001 | 2018-11-10 | 2015-02-01 | 3 |
8 | CS006214000001 | 2019-04-10 | 2015-02-01 | 4 |
9 | CS006214000001 | 2019-06-01 | 2015-02-01 | 4 |
ํด์ค:
์ด ์ฝ๋๋ ์์ ์ค๋ช ํ ์์ ์ ๋น์ทํ์ง๋ง, ๊ณ ๊ฐ์ ์ ์ฒญ์ผ๋ก๋ถํฐ ๊ตฌ๋งค์ผ๊น์ง์ ๊ฒฝ๊ณผ ์ผ์๋ ๊ฒฝ๊ณผ ์์๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ด ์๋๋ผ ๊ฒฝ๊ณผ ๋ ์๋ฅผ ๊ณ์ฐํ๋ ์ฝ๋์ ๋๋ค.
์๋ ์ฝ๋๋ฅผ ์์๋๋ก ์ค๋ช ํฉ๋๋ค.
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates(): df_receipt DataFrame์์ customer_id์ sales_ymd๋ผ๋ ์ปฌ๋ผ๋ง ์ ํํ์ฌ ์ค๋ณต๋๋ ํ์ ๋ชจ๋ ์ญ์ ํ๊ณ ํ์ฌ df_tmp๋ผ๋ ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์์ ์์ฑํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๊ณ ๊ฐ ID์ ํ๋งค์ผ ์กฐํฉ์ด ๊ณ ์ ํ DataFrame์ด ๋ง๋ค์ด์ง๋ค.
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']], how='inner', on='customer_id'): df_tmp์ df_customer DataFrame์ ๋ด๋ถ ๊ฒฐํฉ์ ์ํํ๊ณ , df_customer DataFrame์์ 'customer_id' ์ด๊ณผ 'application_date' ์ด๋ง ์ ํํ๋ค. ๊ฒฐ๊ณผ DataFrame์๋ df_tmp์ df_customer์ ๋ชจ๋ ์กด์ฌํ๋ ๊ณ ๊ฐ ID๋ฅผ ๊ฐ์ง ํ๋ง ํฌํจํ๊ฒ ๋๋ค.
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str')): df_tmp์ "sales_ymd" ์ด์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date']): df_tmp์ "application_date" ์ปฌ๋ผ์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['elapsed_years'] = df_tmp[['sales_ymd', 'application_date']]. \ ฮ apply(lambda x: relativedelta(x[0], x[1]).years, axis=1): "sales_ymd"์ "application_date" ์ด์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ df_tmp์ ์๋ก์ด ์ด "elapsed_years"์ ๋์ ํ๋ค. ์ ๋์ ํ๋ค. ์ด ๊ณ์ฐ์ dateutil ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ relativedelta ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ datetime ๊ฐ์ ์ฐจ์ด๋ฅผ ์ฐ ๋จ์๋ก ๊ณ์ฐํ๊ณ , apply() ๋ฉ์๋๋ "sales_ymd"์ "application_date" ์ด์ ๊ฐ ํ์ ์ด ํจ์๋ฅผ ์ ์ฉํ์ฌ ์ด ๊ฒฝ๊ณผ ์ฐ์๋ฅผ ๋ฐํํ๋ค.
df_tmp.head(10): df_tmp์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ์ฌ ๊ณ์ฐ์ด ์ ๋๋ก ์ด๋ฃจ์ด์ก๋์ง ํ์ธํ๋ค.
์ ์ฒด์ ์ผ๋ก ์ด ์ฝ๋๋ ๊ณ ๊ฐ์ ํ๋์ ๋ถ์ํ๊ณ ๊ณ ๊ฐ์ด ํ์ ๊ฐ์ ์ด๋ ๊ณ์ ์ ์ ์ฒญํ ํ ๊ตฌ๋งคํ๊ธฐ๊น์ง์ ๊ธฐ๊ฐ์ ๊ฒฝ๊ณผ ๋ ์๋ก ํ๋จํ๋ ๋ฐ ์ ์ฉํ๋ค.
ย
P-073: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ์ผ(sales_ymd)์ ๋ํด ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์ ํ์๊ฐ์ ์ผ(application_date)๋ก๋ถํฐ์ ์ํฌํฌ ์ด ๋จ์์ ๊ฒฝ๊ณผ ์๊ฐ์ ๊ณ์ฐํ์ฌ ๊ณ ๊ฐ ID(customer_id), ๋งค์ถ์ผ, ํ์๊ฐ์ ์ผ๊ณผ ํจ๊ป 10๊ฑด์ ํ์ํ๋ค. ๊ณผ ํจ๊ป 10๊ฑด์ ํ์ํ๋ผ(๋จ, sales_ymd๋ ์์น, application_date๋ ๋ฌธ์์ด๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด์ ํ๊ณ ์๋ค๋ ์ ์ ์ ์). ๋จ, ์๊ฐ ์ ๋ณด๋ ๋ณด์ ํ์ง ์์ผ๋ฏ๋ก ๊ฐ ๋ ์ง๋ 0์ 0๋ถ 0์ด๋ก ํ์ํ๋ค.
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates()
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']],
how='inner', on='customer_id')
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str'))
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date'])
df_tmp['elapsed_epoch'] = df_tmp['sales_ymd'].view(np.int64) - \
df_tmp['application_date'].view(np.int64)
df_tmp['elapsed_epoch'] = df_tmp['elapsed_epoch'] / 10**9
df_tmp.head(10)
ย | customer_id | sales_ymd | application_date | elapsed_epoch |
---|---|---|---|---|
0 | CS006214000001 | 2018-11-03 | 2015-02-01 | 118454400.0 |
1 | CS006214000001 | 2017-05-09 | 2015-02-01 | 71539200.0 |
2 | CS006214000001 | 2017-06-08 | 2015-02-01 | 74131200.0 |
3 | CS006214000001 | 2018-10-28 | 2015-02-01 | 117936000.0 |
4 | CS006214000001 | 2019-09-08 | 2015-02-01 | 145152000.0 |
5 | CS006214000001 | 2018-01-31 | 2015-02-01 | 94608000.0 |
6 | CS006214000001 | 2017-07-05 | 2015-02-01 | 76464000.0 |
7 | CS006214000001 | 2018-11-10 | 2015-02-01 | 119059200.0 |
8 | CS006214000001 | 2019-04-10 | 2015-02-01 | 132105600.0 |
9 | CS006214000001 | 2019-06-01 | 2015-02-01 | 136598400.0 |
ํด์ค:
์ด ์ฝ๋๋ datetime์ ๊ฐ์ ์ ๋์ค ํ์์คํฌํ๋ก ๋ณํํ์ฌ ๊ณ ๊ฐ์ ์ ์ฒญ์ผ๊ณผ ๊ตฌ๋งค์ผ ์ฌ์ด์ ๊ฒฝ๊ณผ ์๊ฐ(์ด)์ ๊ณ์ฐํ๋ ์ฝ๋์ ๋๋ค.
๋ค์์ ์ฝ๋์ ๋จ๊ณ๋ณ ์์์ ๋๋ค.
df_tmp = df_receipt[['customer_id', 'sales_ymd']].drop_duplicates(): df_receipt DataFrame์์ "customer_id"์ "sales_ymd" ์ปฌ๋ผ๋ง ์ ํํด์ df_tmp๋ผ๋ ์๋ก์ด DataFrame์ ์์ฑํ๊ณ , ์ค๋ณต๋ ํ์ ์ญ์ ํฉ๋๋ค. ๊ทธ ๊ฒฐ๊ณผ ๊ณ ๊ฐ ID์ ํ๋งค์ผ ์กฐํฉ์ด ๊ณ ์ ํ DataFrame์ด ๋ง๋ค์ด์ก๋ค.
df_tmp = pd.merge(df_tmp, df_customer[['customer_id', 'application_date']], how='inner', on='customer_id'): df_tmp์ df_customer DataFrame์ ๋ด๋ถ ๊ฒฐํฉ์ ์ํํ๊ณ , df_customer DataFrame์์ 'customer_id' ์ด๊ณผ 'application_date' ์ด๋ง ์ ํํ๋ค. ๊ฒฐ๊ณผ DataFrame์๋ df_tmp์ df_customer์ ๋ชจ๋ ์กด์ฌํ๋ ๊ณ ๊ฐ ID๋ฅผ ๊ฐ์ง ํ๋ง ํฌํจํ๊ฒ ๋๋ค.
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str')): df_tmp์ "sales_ymd" ์ด์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['application_date'] = pd.to_datetime(df_tmp['application_date']): df_tmp์ "application_date" ์ปฌ๋ผ์ pd.to_datetime() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ datetime ํ์์ผ๋ก ๋ณํํ๋ค.
df_tmp['elapsed_epoch'] = df_tmp['sales_ymd'].view(np.int64) - \ df_tmp['application_date'].view(np.int64): "sales_ymd" ์ "application_date" ์ด์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํ๋ค. date" ์ด์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ df_tmp์ "elapsed_epoch" ์ด์ ํ ๋นํ๋ค. ์ด ๊ณ์ฐ์ view() ๋ฉ์๋์์ datetime ๊ฐ์ int64 ํ์(Unix ํ์์คํฌํ)์ผ๋ก ๋ณํํ๊ณ , ๋ ๊ฐ์ ์์๋ณ๋ก ๊ฐ์ฐํ์ฌ ๋๋ ธ์ด ๋จ์์ ๊ฒฝ๊ณผ ์๊ฐ์ ๊ตฌํ๊ณ ์๋ค.
df_tmp['elapsed_epoch'] = df_tmp['elapsed_epoch'] / 10**9: "elapsed_epoch" ์ด์ 10^9๋ก ๋๋์ด ๊ฒฝ๊ณผ ์๊ฐ์ ๋๋ ธ์ด์์ ์ด๋ก ๋ณํํ๋ค.
df_tmp.head(10): df_tmp์ ์ฒ์ 10์ค์ ํ์ํ์ฌ ๊ณ์ฐ์ด ์ ๋๋ก ์ด๋ฃจ์ด์ก๋์ง ํ์ธํ๋ค.
์ ์ฒด์ ์ผ๋ก ์ด ์ฝ๋๋ ๊ณ ๊ฐ์ ํ๋์ ๋ถ์ํ๊ณ ๊ณ ๊ฐ์ด ํ์ ๊ฐ์ ์ด๋ ๊ณ์ ์ ์ ์ฒญํ ํ ๊ตฌ๋งคํ๊ธฐ๊น์ง์ ์๊ฐ์ ์ด ๋จ์๋ก ํ๋จํ๋ ๋ฐ ์ ์ฉํ๋ค.
P-074: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ์ผ(sales_ymd)์ ๋ํด ํด๋น ์ฃผ ์์์ผ๋ถํฐ์ ๊ฒฝ๊ณผ์ผ์๋ฅผ ๊ณ์ฐํ์ฌ ๋งค์ถ์ผ, ์ง์ ์์์ผ๊น์ง 10๊ฑด์ฉ ํ์ํ๋ผ(sales_ymd๋ ์์น๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๊ณ ์๋ค๋ ์ ์ ์ ์).
df_tmp = df_receipt[['sales_ymd']].copy()
df_tmp['sales_ymd'] = pd.to_datetime(df_tmp['sales_ymd'].astype('str'))
df_tmp['elapsed_days'] = df_tmp['sales_ymd'].apply(lambda x:x.weekday())
df_tmp['monday'] = \
df_tmp['sales_ymd'].apply(lambda x: x - relativedelta(days=x.weekday()))
df_tmp.head(10)
ย | sales_ymd | elapsed_days | monday |
---|---|---|---|
0 | 2018-11-03 | 5 | 2018-10-29 |
1 | 2018-11-18 | 6 | 2018-11-12 |
2 | 2017-07-12 | 2 | 2017-07-10 |
3 | 2019-02-05 | 1 | 2019-02-04 |
4 | 2018-08-21 | 1 | 2018-08-20 |
5 | 2019-06-05 | 2 | 2019-06-03 |
6 | 2018-12-05 | 2 | 2018-12-03 |
7 | 2019-09-22 | 6 | 2019-09-16 |
8 | 2017-05-04 | 3 | 2017-05-01 |
9 | 2019-10-10 | 3 | 2019-10-07 |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ๋จ๊ณ๋ฅผ ์ํํ๊ณ ์์ต๋๋ค.
๊ธฐ์กด DataFrame df_receipt์์ sales_ymd ์ปฌ๋ผ์ ์ ํํ๊ณ ํด๋น ์ปฌ๋ผ์ ๋ณต์ฌ๋ณธ์ ์์ฑํ์ฌ df_tmp๋ผ๋ ์๋ก์ด DataFrame์ ์์ฑํ๊ณ ์์ต๋๋ค.
๊ทธ๋ฐ ๋ค์ df_tmp์ sales_ymd ์ปฌ๋ผ์ pd.to_datetime ํจ์๋ฅผ ์ฌ์ฉํ์ฌ pandas์ datetime ํ์์ผ๋ก ๋ณํ๋ฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์์ผ ๋ฑ์ ์ ๋ณด๋ฅผ ์ถ์ถํ ์ ์๋ ๋ฑ ๋ณด๋ค ์ ์ฐํ ๋ฐฉ์์ผ๋ก ๋ ์ง๋ฅผ ๋ค๋ฃฐ ์ ์๊ฒ ๋๋ค.
df_tmp์๋ apply ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ elapsed_days๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ด ์์ฑ๋ฉ๋๋ค. ์ด ๋ฉ์๋๋ sales_ymd ์ด์ ๊ฐ ํ์ ๋๋ค ํจ์๋ฅผ ์ ์ฉํ๊ณ , ์ด ๊ฒฝ์ฐ ์์ผ์ ์ ์ ๊ฐ์ผ๋ก ๋ฐํํ๋ค(์์์ผ์ 0, ์ผ์์ผ์ 6์ด๋ค).
apply ๋ฉ์๋์ ๋ ๋ค๋ฅธ ๋๋ค ํจ์๋ฅผ ์ฌ์ฉํ์ฌ df_tmp์ new column named monday๋ฅผ ์์ฑํ๋ค. ์ด ํจ์๋ sales_ymd ์ด์ ๊ฐ ํ์ ๋ฐ์ ์ฃผ์ด๋ถํฐ ๊ฒฝ๊ณผํ ์ผ์(x.weekday()์์ ์ป์)๋ฅผ ๋นผ๊ณ , ๊ทธ ๊ฒฐ๊ณผ ๋ ์ง๋ฅผ ๊ฐ์ฅ ์ต๊ทผ ์์์ผ๋ก ๋ฐ์ฌ๋ฆผํ์ฌ ๋ฐํํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด df_tmp์ ๋ชจ๋ ๋ ์ง๊ฐ ์์์ผ์ ๊ธฐ์ ์ผ๋ก ํ ์ฃผ ๋จ์๋ก ํจ๊ณผ์ ์ผ๋ก ๊ทธ๋ฃนํ๋ฉ๋๋ค.
๋ง์ง๋ง์ผ๋ก head ๋ฉ์๋๊ฐ df_tmp์์ ํธ์ถ๋์ด ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10ํ์ด ํ์๋ฉ๋๋ค.
์ ์ฒด์ ์ผ๋ก ์ด ์ฝ๋๋ ํ๋งค์ผ ์ด์ ์ฒ๋ฆฌํ์ฌ ๋ถ์ ๋๋ ์ง๊ณ ๋ชฉ์ ์ผ๋ก ์ ์ฉํ ์ ๋ณด๋ฅผ ์ถ์ถํ๊ณ ์์ต๋๋ค. ๊ตฌ์ฒด์ ์ผ๋ก DataFrame์ ์์ผ๊ณผ ํด๋น ์ฃผ์ ์์์ผ์ ๋ํ๋ด๋ ์๋ก์ด ์ด์ ์์ฑํ์ฌ ์ฃผ ๋๋ ์์ผ๋ณ๋ก ํ๋งค ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋ฃนํํ๊ฑฐ๋ ์ง๊ณํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ย
P-075: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์์ ๋ฌด์์๋ก 1%์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ์ฌ ๋งจ ์๋ถํฐ 10๊ฐ๋ฅผ ํ์ํ๋ผ.
df_customer.sample(frac=0.01).head(10)
ย | customer_id | customer_name | gender_cd | gender | birth_day | age | postal_cd | address | application_store_cd | application_date | status_cd |
---|---|---|---|---|---|---|---|---|---|---|---|
15707 | CS003702000032 | ๆพๅฑฑ ๅ ๅ | 0 | ็ทๆง | 1943-12-24 | 75 | 214-0001 | ็ฅๅฅๅท็ๅทๅดๅธๅคๆฉๅบ่ ********** | S13003 | 20160422 | 0-00000000-0 |
13320 | CS014712000023 | ๅคงๅฑฑ ๅค็ฉบ | 1 | ๅฅณๆง | 1940-11-02 | 78 | 264-0024 | ๅ่็ๅ่ๅธ่ฅ่ๅบ้ซๅ็บ********** | S12014 | 20150630 | 0-00000000-0 |
2995 | CS028402000059 | ๅฑฑๅฃ ๅฏๆฒป | 0 | ็ทๆง | 1970-06-07 | 48 | 245-0009 | ็ฅๅฅๅท็ๆจชๆตๅธๆณๅบๆฐๆฉ็บ********** | S14028 | 20150508 | 0-00000000-0 |
5927 | CS007313000191 | ๆข ๆฒข ้บป็ท | 1 | ๅฅณๆง | 1984-12-29 | 34 | 285-0858 | ๅ่็ไฝๅๅธใฆใผใซใชใไธ********** | S12007 | 20180913 | 0-00000000-0 |
20923 | CS017713000004 | ๅท็ฌ ๆช่ฏๅญ | 1 | ๅฅณๆง | 1947-12-12 | 71 | 166-0003 | ๆฑไบฌ้ฝๆไธฆๅบ้ซๅๅฏบๅ********** | S13017 | 20141218 | 0-00000000-0 |
13579 | CS004412000576 | ้ๆพ ็ๅธ | 1 | ๅฅณๆง | 1970-05-29 | 48 | 167-0051 | ๆฑไบฌ้ฝๆไธฆๅบ่ป็ชช********** | S13004 | 20170501 | 0-00000000-0 |
5845 | CS025512000044 | ไธๆ ่ใ ็พ | 1 | ๅฅณๆง | 1963-03-08 | 56 | 242-0014 | ็ฅๅฅๅท็ๅคงๅๅธไธๅ็ฐ********** | S14025 | 20150222 | 3-20080930-4 |
21563 | CS001412000408 | ๅคๆฒข ใพใใฟ | 1 | ๅฅณๆง | 1976-08-13 | 42 | 210-0852 | ็ฅๅฅๅท็ๅทๅดๅธๅทๅดๅบ้ผ็ฎก้********** | S13001 | 20160102 | 0-00000000-0 |
8983 | CS004313000089 | ๅฟๆฐด ้ฝๅญ | 1 | ๅฅณๆง | 1983-07-06 | 35 | 176-0014 | ๆฑไบฌ้ฝ็ทด้ฆฌๅบ่ฑ็ๅ********** | S13004 | 20151010 | 0-00000000-0 |
703 | CS018512000192 | ๅฏบ่ฅฟ ้ๆต | 1 | ๅฅณๆง | 1965-09-29 | 53 | 204-0002 | ๆฑไบฌ้ฝๆธ ็ฌๅธๆญใไธ********** | S13018 | 20180528 | 0-00000000-0 |
ํด์ค:
์ด ์ฝ๋๋ df_customer๋ผ๋ pandas์ DataFrame์์ ํ์ ํ์ ์งํฉ์ ์ํ๋งํ์ฌ ๊ทธ ํ์ ์งํฉ์ ์ฒซ 10๊ฐ์ ํ์ ํ์ํ๋ ์ฝ๋์ ๋๋ค.
์๋๋ ์ฝ๋์ ๊ฐ ๋ถ๋ถ์ด ํ๋ ์ผ์ด๋ค.
df_customer๋ ๊ณ ๊ฐ ๋ฐ์ดํฐ๊ฐ ํฌํจ๋ pandas์ DataFrame์ผ๋ก, ์๋ง๋ ๋ง์ ํ๊ณผ ์ด์ด ์์ ๊ฒ์ด๋ค.
sample ๋ฉ์๋๋ df_customer์ ๋ํด frac=0.01์ด๋ผ๋ ์ธ์๋ก ํธ์ถ๋๋ค. ์ด ๋ฉ์๋๋ DataFrame์ ์ผ๋ถ ํ์ ๋ฌด์์๋ก ์ ํํ๋ค. ์ฌ๊ธฐ์ frac์ ์ํ๋งํ ํ์ ๋น์จ์ ๋ํ๋ธ๋ค. ์ด ๊ฒฝ์ฐ frac=0.01์ df_customer์ ํ ์ค 1%๊ฐ ๋ฌด์์๋ก ์ ํ๋จ์ ์๋ฏธํ๋ค.
head ๋ฉ์๋๋ df_customer ๊ฒฐ๊ณผ์ ํ์ ์งํฉ์ ๋ํด ์ธ์ 10์ผ๋ก ํธ์ถ๋๋ค. ์ด ๋ฉ์๋๋ ํ์ ์งํฉ์ ์ฒ์ 10๊ฐ์ ํ์ ๋ฐํํ๊ณ ์ด๋ฅผ ํ์ํฉ๋๋ค.
์ฆ, ์ด ์ฝ๋๋ ์ ์ฒด์ ์ผ๋ก df_customer์์ 1%์ ๋ฌด์์ ์ํ์ ์ ํํ๊ณ ๊ทธ ์ํ์ ์ฒ์ 10๊ฐ์ ํ์ ํ์ํ๊ณ ์๋ค. ์ด๋ ์ ์ฒด ๋ฐ์ดํฐ ์ธํธ๋ฅผ ์กฐ์ํ์ง ์๊ณ ๋ ๋ฐ์ดํฐ์ ์์ ํ์ ์งํฉ์ ๋ํด ๋ฐ์ดํฐ๋ฅผ ํ์ํ๊ฑฐ๋ ๋น ๋ฅธ ๋ถ์์ ์ํํ๋ ๋ฐ ์ ์ฉํ๋ค.
ย
P-076: ๊ณ ๊ฐ ๋ฐ์ดํฐ(df_customer)์์ ์ฑ๋ณ ์ฝ๋(gender_cd)์ ๋น์จ์ ๋ฐ๋ผ ๋ฌด์์๋ก 10%์ ๋ฐ์ดํฐ๋ฅผ ์ธตํ ์ถ์ถํ๊ณ , ์ฑ๋ณ ์ฝ๋๋ณ๋ก ๊ฑด์๋ฅผ ์ง๊ณํ๋ผ.
# sklearn.model_selection.train_test_split ์ฌ์ฉ ์์
_, df_tmp = train_test_split(df_customer, test_size=0.1,
stratify=df_customer['gender_cd'])
df_tmp.groupby('gender_cd').agg({'customer_id' : 'count'})
ย | customer_id |
---|---|
gender_cd | ย |
0 | 298 |
1 | 1793 |
9 | 107 |
ํด์ค:
์ด ์ฝ๋๋ ๋ค์ ๋จ๊ณ๋ฅผ ์ํํ๋ค.
train_test_split์ scikit-learn ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํจ์๋ก, ๋ฐ์ดํฐ ์ธํธ๋ฅผ ๋ฌด์์๋ก ๋ ๊ฐ์ ํ์ ์งํฉ('ํ๋ จ' ํ์ ์งํฉ๊ณผ 'ํ ์คํธ' ํ์ ์งํฉ)์ผ๋ก ๋ถํ ํ๋ ํจ์์ ๋๋ค. ์ด ์ฝ๋์์๋ df_customer๋ผ๋ pandas์ DataFrame์ ์ ์ฉํ๊ณ , ์ธ์ test_size=0.1๋ก ๋ฐ์ดํฐ์ 10%๋ฅผ ํ ์คํธ์ฉ์ผ๋ก ํ๋ณดํ ๊ฒ์ ์ง์ ํ๊ณ , ์ธ์ stratify=df_customer['gender_cd']๋ก gender_cd ์ด์ ๊ฐ์ผ๋ก ๊ณ์ธตํํ ๊ฒ์ ์ง์ ํ์ต๋๋ค. ์ด ํจ์๋ ๋ ๊ฐ์ DataFrame์ ๋ฐํํ์ง๋ง, ์ฒซ ๋ฒ์งธ DataFrame์ ๋ฌด์๋๋ค(_๊ฐ ํ ๋น๋๋๋ฐ, ์ด๋ ํ์ด์ฌ์ ๊ดํ์ผ๋ก, ์ฌ์ฉ๋์ง ์์ ๋ณ์๋ฅผ ๋ํ๋ด๋ ๊ฒ์ด๋ค).
train_test_split์ด ๋ฐํํ๋ ๋ ๋ฒ์งธ DataFrame์ df_tmp๋ผ๋ ์๋ก์ด ๋ณ์์ ํ ๋น๋๋ค.
df_tmp์ ๋ํด gender_cd๋ผ๋ ์ธ์๋ก groupby ๋ฉ์๋๊ฐ ํธ์ถ๋๋ค. ์ด ๋ฉ์๋๋ df_tmp์ ํ์ gender_cd ์ด์ ๊ฐ์ผ๋ก ๊ทธ๋ฃนํํ๋ค.
agg ๋ฉ์๋๋ groupby ์ฐ์ฐ ๊ฒฐ๊ณผ์ ๋ํด {'customer_id' : 'count'}๋ผ๋ ์ธ์๋ก ํธ์ถ๋๋ค. ์ด ๋ฉ์๋๋ ๊ฐ ๊ทธ๋ฃน์ customer_id ์ปฌ๋ผ์ ํจ์(์ฌ๊ธฐ์๋ count)๋ฅผ ์ ์ฉํ๊ณ , ๊ฐ ๊ทธ๋ฃน์ ๊ฒฐ๊ณผ ์นด์ดํธ๋ฅผ ๊ฐ์ง DataFrame์ ๋ฐํํ๋ค.
์์ฑ๋ DataFrame์ด ํ์๋๋ค.
์ฆ, ์ด ์ฝ๋๋ ์ ์ฒด์ ์ผ๋ก pandas์ DataFrame์ ํ๋ จ์ฉ๊ณผ ํ ์คํธ์ฉ์ผ๋ก ๋ฌด์์๋ก ๋ถํ ํ๊ณ ํน์ ์ด(gender_cd)์ ๊ฐ์ ๋ฐ๋ผ ๊ณ์ธตํํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ ์คํธ์ฉ ์๋ธ์ ์์ ์๋ก์ด DataFrame์ ์์ฑํ์ฌ gender_cd ์ด๋ก ๊ทธ๋ฃนํํ์ฌ ๊ฐ ๊ทธ๋ฃน์ customer_id ์ด์ ๊ณ ์ ํ ๊ฐ์ ๊ฐ์๋ฅผ ๊ณ์ฐํ ์ ์์ต๋๋ค. ์ด๋ ํ ์คํธ์ฉ ์๋ธ์ ์ ๋ฐ์ดํฐ์์ ์ฑ๋ณ์ ๋ฐ๋ฅธ ๊ณ ๊ฐ ๋ถํฌ๋ฅผ ํ์ ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
P-077: ์์์ฆ ๋ช ์ธ์ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก์ ๊ณ ๊ฐ ๋จ์๋ก ํฉ์ฐํ๊ณ , ํฉ์ฐํ ๋งค์ถ ๊ธ์ก์ ํธ์ฐจ๋ฅผ ์ถ์ถํ๋ผ. ๋จ, ์ด์๊ฐ์ ๋งค์ถ๊ธ์ก ํฉ๊ณ๋ฅผ ๋ก๊ทธํํ ํ ํ๊ท ๊ณผ ํ์คํธ์ฐจ๋ฅผ ๊ณ์ฐํ์ฌ ๊ทธ ํ๊ท ์์ 3ฯ ์ด์ ๋ฒ์ด๋ ๊ฒ์ผ๋ก ํ๋ค(์์ฐ๋์, ์์ฉ๋์ ๋ชจ๋ ๊ฐ๋ฅ). ๊ฒฐ๊ณผ๋ 10๊ฑด ํ์ํ๋ผ.
df_sales_amount = df_receipt.groupby('customer_id'). \
agg({'amount':'sum'}).reset_index()
df_sales_amount['log_sum_amount'] = np.log(df_sales_amount['amount'] + 0.5)
df_sales_amount['log_sum_amount_ss'] = preprocessing.scale(df_sales_amount['log_sum_amount'])
df_sales_amount.query('abs(log_sum_amount_ss) > 3').head(10)
ย | customer_id | amount | log_sum_amount | log_sum_amount_ss |
---|---|---|---|---|
8306 | ZZ000000000000 | 12395003 | 16.332804 | 7.967822 |
ํด์ค:
์ด ์ฝ๋๋ df_receipt๋ผ๋ pandas DataFrame ๊ฐ์ฒด์ ๋ํด ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ ๋ฐ ํํฐ๋ง์ ์ํํ๊ณ ์๋ค.
๋ค์์ ์ฝ๋์ ๋จ๊ณ๋ณ ์ค๋ช ์ ๋๋ค.
df_sales_amount = df_receipt.groupby('customer_id').agg({'amount':'sum'}).reset_index(): ์ด ํ์ df_receipt์ ํ์ customer_id ์ด์ ๊ฐ์ผ๋ก ๊ทธ๋ฃนํํ๊ณ , ๊ฐ ๊ทธ๋ฃน์ amount ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํฉ๋๋ค. ๊ทธ๋ฃน์ amount ์ด์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ๊ณ ์๋ค. ๊ฒฐ๊ณผ DataFrame df_sales_amount๋ ๊ณ ์ ํ customer_id ๊ฐ๋ง๋ค 1ํ, customer_id์ amount์ 2์ด์ ๊ฐ์ง๋ฉฐ, reset_index() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ทธ๋ฃนํ ํ ์ธ๋ฑ์ค๋ฅผ ์ ์๊ฐ์ผ๋ก ์ฌ์ค์ ํ์ฌ ํ๊ณ ์์ต๋๋ค.
df_sales_amount['log_sum_amount'] = np.log(df_sales_amount['amount'] + 0.5): ์ด ํ์ df_sales_amount์ log_sum_amount๋ผ๋ ์๋ก์ด ์ปฌ๋ผ์ ์์ฑํ๊ณ , ๊ฐ ๊ณ ๊ฐ๋ณ ๊ธ์ก์ ํฉ๊ณ ์ ์์ฐ๋์, ๊ทธ๋ฆฌ๊ณ 0์ ๋์๋ฅผ ํผํ๊ธฐ ์ํด 0.5๋ฅผ ๋ํ ๊ฐ์ ์ ์ฅํฉ๋๋ค.
df_sales_amount['log_sum_amount_ss'] = preprocessing.scale(df_sales_amount['log_sum_amount']): ์ด ํ์ df_sales_amount์ log_sum_amount_ss ๋ผ๋ ์๋ก์ด ์ด์ ๋ง๋ค๊ณ , scikit-learn ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ preprocessing.scale ํจ์๋ฅผ ์ฌ์ฉํ์ฌ log_sum_amount๋ฅผ ์ค์ผ์ผ๋งํ ๊ฐ์ ์ ์ฅํ๋ค. ์ด ํจ์๋ ์ ๋ ฅ ๋ฐฐ์ด์ ํ๊ท ์ ๋นผ๊ณ ํ์คํธ์ฐจ๋ก ๋๋์ด ํ๊ท ์ด 0์ด๊ณ ๋ถ์ฐ์ด 0์ธ ๋จ์์ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ ๋ค.
df_sales_amount.query('abs(log_sum_amount_ss) > 3').head(10): ์ด ํ์ log_sum_amount_ss์ ์ ๋๊ฐ์ด 3๋ณด๋ค ํฐ ํ๋ง ๋จ๋๋ก df_sales_amount๋ฅผ ํํฐ๋งํ๊ณ ๊ฒฐ๊ณผ์ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ์ ํํ๋ค. ์ด ํํฐ๋ ์ด๋งค์ถ์ก์ ํ๊ท ๊ฐ์์ ํ์คํธ์ฐจ๊ฐ 3 ์ด์ ๋จ์ด์ ธ ์๋ ๊ณ ๊ฐ์ ์ ํํ์ฌ ์ด์๊ฐ์ผ ๊ฐ๋ฅ์ฑ์ ํ์ํ๊ณ ์์ผ๋ฉฐ, query ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด ํํ์(์ด ๊ฒฝ์ฐ 'abs(log_sum_amount_ss) > 3')์ ๊ธฐ์ค์ผ๋ก ํ์ ํํฐ๋งํ๊ณ ์๋ค.
ย
P-078: ์์์ฆ ๋ด์ญ ๋ฐ์ดํฐ(df_receipt)์ ๋งค์ถ ๊ธ์ก(amount)์ ๊ณ ๊ฐ ๋จ์๋ก ํฉ์ฐํ๊ณ , ํฉ์ฐ๋ ๋งค์ถ ๊ธ์ก์ ์ธ๊ณฝ๊ฐ์ ์ถ์ถํ๋ค. ๋จ, ๊ณ ๊ฐ ID๊ฐ โZโ๋ก ์์ํ๋ ๊ฒ์ ๋นํ์์ ์๋ฏธํ๋ฏ๋ก ์ ์ธํ์ฌ ๊ณ์ฐํ๋ค. ์ฌ๊ธฐ์ ์ด์๊ฐ์ 1์ฌ๋ถ์์ 3์ฌ๋ถ์์ ์ฐจ์ด์ธ IQR์ ์ด์ฉํ์ฌ โ1์ฌ๋ถ์์ -1.5รIQRโ ์ดํ ๋๋ โ3์ฌ๋ถ์์+1.5รIQRโ์ ์ด๊ณผํ๋ ๊ฒ์ผ๋ก ํ๋ค. ๊ฒฐ๊ณผ๋ 10๊ฑด ํ์ํ๋ค.
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")',
engine='python'). \
groupby('customer_id'). \
agg({'amount':'sum'}).reset_index()
pct25 = np.percentile(df_sales_amount['amount'], q=25)
pct75 = np.percentile(df_sales_amount['amount'], q=75)
iqr = pct75 - pct25
amount_low = pct25 - (iqr * 1.5)
amount_hight = pct75 + (iqr * 1.5)
df_sales_amount.query('amount < @amount_low or @amount_hight < amount').head(10)
ย | customer_id | amount |
---|---|---|
98 | CS001414000048 | 8584 |
332 | CS001605000009 | 18925 |
549 | CS002415000594 | 9568 |
1180 | CS004414000181 | 9584 |
1558 | CS005415000137 | 8734 |
1733 | CS006414000001 | 9156 |
1736 | CS006414000029 | 9179 |
1752 | CS006415000105 | 10042 |
1755 | CS006415000147 | 12723 |
1757 | CS006415000157 | 10648 |
ํด์ค:
์ด ์ฝ๋๋ df_receipt๋ผ๋ pandas DataFrame ๊ฐ์ฒด์ ๋ํด ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ ๋ฐ ํํฐ๋ง์ ์ํํ๊ณ ์๋ค.
๋ค์์ ์ฝ๋์ ๋จ๊ณ๋ณ ์ค๋ช ์ ๋๋ค.
df_sales_amount = df_receipt.query('not customer_id.str.startswith("Z")', engine='python').groupby('customer_id').agg({'amount':'sum '}).reset_index(): ์ด ํ์ ๋จผ์ df_receipt๋ฅผ ํํฐ๋งํ์ฌ customer_id ์ด์ด 'Z'๋ก ์์ํ๋ ํ์ ์ ์ธํ๊ณ ์์ผ๋ฉฐ, query ๋ฉ์๋๋ ๋ฌธ์์ด ํํ์์ ๊ธฐ๋ฐ์ผ๋ก ํ์ ํํฐ๋งํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. python' ์ธ์๋ ์ฟผ๋ฆฌ์ Python ์์ง์ ์ฌ์ฉํ๋๋ก ์ง์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, str.startswith ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ๊ฒฐ๊ณผ DataFrame์ customer_id๋ก ๊ทธ๋ฃนํ๋๋ฉฐ, ๊ทธ๋ฃน๋ณ๋ก ๊ธ์ก ์ด์ ํฉ๊ณ๊ฐ ๊ณ์ฐ๋๋ค. ๊ฒฐ๊ณผ DataFrame df_sales_amount๋ ๊ณ ์ ํ customer_id ๊ฐ๋ง๋ค ํ ์ค, customer_id์ amount ๋ ๊ฐ์ ์ปฌ๋ผ์ ๊ฐ์ง๋ฉฐ, reset_index() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ทธ๋ฃนํ ํ ์ธ๋ฑ์ค๋ฅผ ์ ์ ๊ฐ์ผ๋ก ์ฌ์ค์ ํ๊ณ ์๋ค.
pct25 = np.percentile(df_sales_amount['amount'], q=25): ์ด ํ์ numpy.percentile ํจ์๋ฅผ ์ฌ์ฉํ์ฌ df_sales_amount์ amount ์ปฌ๋ผ์ 25๋ฒ์งธ ๋ฐฑ๋ถ์์๋ฅผ ๊ณ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ณ์ pct25์ ๋์ ํ๊ณ ์์ต๋๋ค.
pct75 = np.percentile(df_sales_amount['amount'], q=75): ์ด ํ์ numpy.percentile ํจ์๋ฅผ ์ฌ์ฉํ์ฌ df_sales_amount์ amount ์ด์ 75๋ฒ์งธ ๋ฐฑ๋ถ์์๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ pct75 ๋ณ์์ ๋์ ํ๋ค. ๋ณ์์ ๋์ ํฉ๋๋ค.
iqr = pct75 - pct25: ์ด ํ์ 75๋ฐฑ๋ถ์์(pct75)์์ 25๋ฐฑ๋ถ์์(pct25)๋ฅผ ๋นผ์ df_sales_amount์ ๊ธ์ก ์ด์ ์ฌ๋ถ์์ ๋ฒ์(IQR)๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ iqr ๋ณ์์ ๋์ ํ๊ณ ์๋ค.
amount_low = pct25 - (iqr * 1.5): ์ด ํ์ amount_low = pct25 - (iqr * 1.5) ๊ณต์์ ์ฌ์ฉํ์ฌ ํ๋งค ๊ธ์ก์ "์ ์" ๋ฒ์์ ํํ์ ๊ณ์ฐํ๋ค. ์ด๊ฒ์ ์ผ๋ฐ์ ์ผ๋ก ๋ฐ์คํ ํ๋กฏ์ ์๋์ชฝ "์์ผ"์ผ๋ก ์๋ ค์ ธ ์๋ค.
amount_high = pct75 + (iqr * 1.5). ์ด ํ์ amount_high = pct75 + (iqr * 1.5) ๊ณต์์ ์ฌ์ฉํ์ฌ ํ๋งค ๊ธ์ก์ "์ ์" ๋ฒ์์ ์ํ์ ๊ณ์ฐํ๋ค. ์ด๊ฒ์ ์ผ๋ฐ์ ์ผ๋ก ์์ ์์ผ ๋ํ์ ์ํ "์์ผ"์ผ๋ก ์๋ ค์ ธ ์๋ค.
df_sales_amount.query('amount < @amount_low or @amount_high < amount').head(10): ์ด ํ์ amount ์ด์ด amount_low๋ณด๋ค ์๊ฑฐ๋ amount_high๋ณด๋ค ํฐ ํ๋ง ๋จ๊ฒจ๋๋๋ก df sales_amount๋ฅผ ํํฐ๋งํ์ฌ ๊ฒฐ๊ณผ DataFrame์ ์ฒ์ 10๊ฐ์ ํ์ ์ ํํ๋ค. ์ด ํํฐ๋ ๋งค์ถ ๊ธ์ก ํฉ๊ณ๊ฐ ๋งค์ถ ๊ธ์ก์ '์ ์' ๋ฒ์๋ฅผ ๋ฒ์ด๋ ๊ณ ๊ฐ์ ์ ํํ๋ ๊ฒ์ผ๋ก, ๋ฐ์ค ํ๋กฏ์ ์๋์ชฝ ์์ผ๊ณผ ์์ชฝ ์์ผ ์ฌ์ด์ ๋ฒ์๋ก ์ ์๋๋ฉฐ, Python์ ๋ณ์ amount_low์ amount_high๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํด @ ๊ธฐํธ๋ฅผ ์ฌ์ฉํ๋ ๋ฌธ์์ด ํํ์์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ค. ํ์ ํํฐ๋งํ๊ธฐ ์ํด query ๋ฉ์๋๊ฐ ์ฌ์ฉ๋์์ต๋๋ค.
P-079: ์ํ ๋ฐ์ดํฐ(df_product)์ ๊ฐ ํญ๋ชฉ์ ๋ํด ๊ฒฐ์ ์๋ฅผ ํ์ธํ๋ผ.
df_product.isnull().sum()
product_cd 0 category_major_cd 0 category_medium_cd 0 category_small_cd 0 unit_price 7 unit_cost 7 dtype: int64
ํด์ค:
์ด ์ฝ๋๋ pandas์ DataFrame df_product์ ๊ฐ ์ด์ ๊ฒฐ์(null) ๊ฐ ์๋ฅผ ๊ณ์ฐํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์๋์์ ๋จ๊ณ๋ณ๋ก ์ค๋ช ํ๋ค.
df_product: ์ฌ์ฉ ์ค์ธ DataFrame์ ์ด๋ฆ์ ๋๋ค.
isnull(): ์ด ๋ฉ์๋๋ df_product์ ๋์ผํ ๋ชจ์์ DataFrame์ ๋ฐํํ๋ฉฐ, ๊ฐ ์์๋ df_product์ ํด๋น ์์๊ฐ null์ธ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ boolean ๊ฐ์ด๋ค.
sum(): isnull()์ด ๋ฐํํ๋ boolean DataFrame์ ์ ์ฉ๋์ด ๊ฐ ์ด์ boolean ๊ฐ์ ํฉ์ ๋ฐํํ๋ ๋ฉ์๋์ด๋ฉฐ, boolean ๊ฐ์ ์ ์๋ก ์ทจ๊ธ๋๋ฏ๋ก ์ฌ์ค์ df_product์ ๊ฐ ์ด์ null ๊ฐ์ ๊ฐ์๋ฅผ ์ธ๋ ๊ฒ๊ณผ ๊ฐ๋ค.
๋ฐ๋ผ์ df_product.isnull().sum()์ ์ธ๋ฑ์ค๊ฐ df_product์ ์ด ์ด๋ฆ, ๊ฐ์ด ๊ฐ ์ด์ NULL ๊ฐ ์์ธ Series ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค. ์ด๋ ๋๋ฝ๋ ๊ฐ์ด ์๋ ์ด๊ณผ ๊ฐ ์ด์ ๋ช ๊ฐ์ ๋๋ฝ๋ ๊ฐ์ด ์๋์ง๋ฅผ ํ์ ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ค.
ย
P-080: ์ํ ๋ฐ์ดํฐ(df_product) ์ค ์ด๋ ํ ํญ๋ชฉ์ ๊ฒฐ์์ด ๋ฐ์ํ ๋ ์ฝ๋๋ฅผ ๋ชจ๋ ์ญ์ ํ ์๋ก์ด ์ํ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ค. ๋ํ, ์ญ์ ์ ํ์ ๊ฑด์๋ฅผ ํ์ํ๊ณ , 079์์ ํ์ธํ ๊ฑด์๋งํผ ๊ฐ์ํ ๊ฒ๋ ํ์ธํด์ผ ํ๋ค.
df_product_1 = df_product.copy()
df_product_1.dropna(inplace=True)
print('์ญ์ ์ :', len(df_product))
print('์ญ์ ํ:', len(df_product_1))
์ญ์ ์ : 10030 ์ญ์ ํ: 10023
ํด์ค:
์ด ์ฝ๋๋ pandas์ DataFrame df_product์์ NULL(๊ฒฐ์) ๊ฐ์ ํฌํจํ๋ ๋ชจ๋ ํ์ ์ ๊ฑฐํ๊ณ , NULL์ด ์๋ ํ๋ง ํฌํจํ๋ ์๋ก์ด DataFrame df_product_1์ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์๋์์ ๋จ๊ณ๋ณ๋ก ์ค๋ช ํ๋ค.
df_product: ์ฌ์ฉํ ์๋ณธ DataFrame์ ์ด๋ฆ์ด๋ค.
df_product.copy(): ์ด ๋ฉ์๋๋ ์๋ณธ DataFrame์ ๋ณต์ฌ๋ณธ์ ์์ฑํ์ฌ ์๋ณธ DataFrame์ด ๋ณ๊ฒฝ๋์ง ์๋๋ก ํ๋ค.
df_product_1: ์์ฑ๋๋ ์๋ก์ด DataFrame์ ์ด๋ฆ์ด๋ค.
dropna(). ์ด ๋ฉ์๋๋ df_product_1์์ null ๊ฐ์ ํฌํจํ ๋ชจ๋ ํ์ ์ญ์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, inplace=True ์ธ์๋ ์๋ก์ด DataFrame์ ๋ฐํํ๋ ๋์ df_product_1์ ๊ทธ ์๋ฆฌ์์ ๋ณ๊ฒฝํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
print('Before deletion:', len(df_product)): null ๊ฐ์ ์ญ์ ํ๊ธฐ ์ ์๋ DataFrame df_product์ ํ ์๋ฅผ ํ์ํ๋ค.
print('After deletion:', len(df_product_1)): ์ด ์ค์ ๋ ๊ฐ์ด ์ญ์ ๋ ํ ์๋ก์ด DataFrame df_product_1์ ํ ์๋ฅผ ํ์ํฉ๋๋ค.
๋ฐ๋ผ์ ์ด ์ฝ๋์์๋ ๋จผ์ ์๋ณธ DataFrame df_product์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค๊ณ , ๋ณต์ฌํ DataFrame์์ NULL ๊ฐ์ ํฌํจํ ๋ชจ๋ ํ์ ์ญ์ ํ์ฌ ์๋ก์ด DataFrame df_product_1์ ๋ง๋ค๊ณ , ๋ง์ง๋ง์ผ๋ก NULL ๊ฐ ์ญ์ ์ ๊ณผ ์ญ์ ํ์ ์๋ณธ DataFrame๊ณผ ์๋ก์ด DataFrame์ ํ ์๋ฅผ ํ์ํฉ๋๋ค. DataFrame๊ณผ ์๋ก์ด DataFrame ๋ด์ ํ ์๋ฅผ ํ์ํ๋ค. ์ด ์ฝ๋๋ NULL ๊ฐ์ผ๋ก ์ธํด ์ญ์ ๋ ํ ์๋ฅผ ํ์ธํ๊ณ NULL ๊ฐ์ ํฌํจํ์ง ์๋ ๊นจ๋ํ DataFrame์ ์์ฑํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ค.
ย
Comment