가상 쇼핑몰 고객 주문 데이터(컬럼,로우) 확인하기

학습목표

  1. 가상 쇼핑몰 고객 주문 데이터 파악하기
    • 현재 상황(데이터로 부터) 파악
    • 모델 수립 혹은 목표 설정

데이터 셋

  • 온라인 리테일 사이트의 2010/12 - 2011/12간의 주문 기록 데이터
  • 약 500,000건의 데이터
  • 데이터 출처: UCI ML Repository
import numpy as np
import pandas as pd
retail = pd.read_csv('./OnlineRetail.csv')

컬럼 확인하기

  • columns 속성으로 확인
  • 컬럼
  • invoiceNo: 주문 번호
  • StockCode: 아이템 아이디
  • Description: 상품 설명
  • Quantity: 상품 주문 수량
  • InvoiceDate: 주문 시각
  • UnitPrice: 상품 가격(동일한 통화)
  • CustomerID: 고객 아이디
  • Country: 고객 거주 지역(국가)
retail.columns
Index(['InvoiceNo', 'StockCode', 'Description', 'Quantity', 'InvoiceDate',
       'UnitPrice', 'CustomerID', 'Country'],
      dtype='object')

데이터 살펴보기

  1. 데이터 분석의 가장 첫 단계
  2. 데이터를 대략적으로 파악 가능(타입, 저장된 형태)
  3. 데이터 cleansing 전략 수립
retail.head()
InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country
0 536365 85123A WHITE HANGING HEART T-LIGHT HOLDER 6 12/1/2010 8:26 2.55 17850.0 United Kingdom
1 536365 71053 WHITE METAL LANTERN 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
2 536365 84406B CREAM CUPID HEARTS COAT HANGER 8 12/1/2010 8:26 2.75 17850.0 United Kingdom
3 536365 84029G KNITTED UNION FLAG HOT WATER BOTTLE 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
4 536365 84029E RED WOOLLY HOTTIE WHITE HEART. 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
retail.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 541909 entries, 0 to 541908
Data columns (total 8 columns):
 #   Column       Non-Null Count   Dtype  
---  ------       --------------   -----  
 0   InvoiceNo    541909 non-null  object 
 1   StockCode    541909 non-null  object 
 2   Description  540455 non-null  object 
 3   Quantity     541909 non-null  int64  
 4   InvoiceDate  541909 non-null  object 
 5   UnitPrice    541909 non-null  float64
 6   CustomerID   406829 non-null  float64
 7   Country      541909 non-null  object 
dtypes: float64(2), int64(1), object(5)
memory usage: 33.1+ MB
retail.describe()
Quantity UnitPrice CustomerID
count 541909.000000 541909.000000 406829.000000
mean 9.552250 4.611114 15287.690570
std 218.081158 96.759853 1713.600303
min -80995.000000 -11062.060000 12346.000000
25% 1.000000 1.250000 13953.000000
50% 3.000000 2.080000 15152.000000
75% 10.000000 4.130000 16791.000000
max 80995.000000 38970.000000 18287.000000

Data cleansing

  • null 데이터 처리
  • CustomerID
  • Business 로직에 맞지 않은 데이터 처리
  • 음수의 아이템 수량
  • 가격이 0원
retail.isnull().sum()
InvoiceNo           0
StockCode           0
Description      1454
Quantity            0
InvoiceDate         0
UnitPrice           0
CustomerID     135080
Country             0
dtype: int64

null customerID 제거

pd.notnull(retail['CustomerID']) #null 아닌 값 true 확인하기
0         True
1         True
2         True
3         True
4         True
          ... 
541904    True
541905    True
541906    True
541907    True
541908    True
Name: CustomerID, Length: 541909, dtype: bool
retail[pd.notnull(retail['CustomerID'])] #null이 아닌 값 retail 데이터 프레임에 넣어준다
InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country
0 536365 85123A WHITE HANGING HEART T-LIGHT HOLDER 6 12/1/2010 8:26 2.55 17850.0 United Kingdom
1 536365 71053 WHITE METAL LANTERN 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
2 536365 84406B CREAM CUPID HEARTS COAT HANGER 8 12/1/2010 8:26 2.75 17850.0 United Kingdom
3 536365 84029G KNITTED UNION FLAG HOT WATER BOTTLE 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
4 536365 84029E RED WOOLLY HOTTIE WHITE HEART. 6 12/1/2010 8:26 3.39 17850.0 United Kingdom
... ... ... ... ... ... ... ... ...
541904 581587 22613 PACK OF 20 SPACEBOY NAPKINS 12 12/9/2011 12:50 0.85 12680.0 France
541905 581587 22899 CHILDREN'S APRON DOLLY GIRL 6 12/9/2011 12:50 2.10 12680.0 France
541906 581587 23254 CHILDRENS CUTLERY DOLLY GIRL 4 12/9/2011 12:50 4.15 12680.0 France
541907 581587 23255 CHILDRENS CUTLERY CIRCUS PARADE 4 12/9/2011 12:50 4.15 12680.0 France
541908 581587 22138 BAKING SET 9 PIECE RETROSPOT 3 12/9/2011 12:50 4.95 12680.0 France

406829 rows × 8 columns

retail = retail[pd.notnull(retail['CustomerID'])]
len(retail)
406829

비지니스 로직에 맞지 않은 데이터 제거

  • 수량, 가격 > 0
retail = retail[retail['Quantity'] > 0]
retail = retail[retail['UnitPrice'] > 0]

len(retail)
397884
retail.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 397884 entries, 0 to 541908
Data columns (total 8 columns):
 #   Column       Non-Null Count   Dtype  
---  ------       --------------   -----  
 0   InvoiceNo    397884 non-null  object 
 1   StockCode    397884 non-null  object 
 2   Description  397884 non-null  object 
 3   Quantity     397884 non-null  int64  
 4   InvoiceDate  397884 non-null  object 
 5   UnitPrice    397884 non-null  float64
 6   CustomerID   397884 non-null  float64
 7   Country      397884 non-null  object 
dtypes: float64(2), int64(1), object(5)
memory usage: 27.3+ MB
retail.describe()
Quantity UnitPrice CustomerID
count 397884.000000 397884.000000 397884.000000
mean 12.988238 3.116488 15294.423453
std 179.331775 22.097877 1713.141560
min 1.000000 0.001000 12346.000000
25% 2.000000 1.250000 13969.000000
50% 6.000000 1.950000 15159.000000
75% 12.000000 3.750000 16795.000000
max 80995.000000 8142.750000 18287.000000

데이터 타입 변경

  • 메모리 효율화
  • 올바른 데이터 타입 매칭
retail['CustomerID'] = retail['CustomerID'].astype(np.int32)
retail.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 397884 entries, 0 to 541908
Data columns (total 8 columns):
 #   Column       Non-Null Count   Dtype  
---  ------       --------------   -----  
 0   InvoiceNo    397884 non-null  object 
 1   StockCode    397884 non-null  object 
 2   Description  397884 non-null  object 
 3   Quantity     397884 non-null  int64  
 4   InvoiceDate  397884 non-null  object 
 5   UnitPrice    397884 non-null  float64
 6   CustomerID   397884 non-null  int32  
 7   Country      397884 non-null  object 
dtypes: float64(1), int32(1), int64(1), object(5)
memory usage: 25.8+ MB
  • memory usage: 27.3+ MB 에서 memory usage: 25.8+ MB 으로 바뀌면서 메모리 사용공간 커짐

새로운 컬럼 추가

  • Quantity * UnitPrice는 고객의 총 지출 비용(CheckoutPrice)
retail['CheckoutPrice'] = retail['UnitPrice'] * retail['Quantity']
retail.head()
InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country CheckoutPrice
0 536365 85123A WHITE HANGING HEART T-LIGHT HOLDER 6 12/1/2010 8:26 2.55 17850 United Kingdom 15.30
1 536365 71053 WHITE METAL LANTERN 6 12/1/2010 8:26 3.39 17850 United Kingdom 20.34
2 536365 84406B CREAM CUPID HEARTS COAT HANGER 8 12/1/2010 8:26 2.75 17850 United Kingdom 22.00
3 536365 84029G KNITTED UNION FLAG HOT WATER BOTTLE 6 12/1/2010 8:26 3.39 17850 United Kingdom 20.34
4 536365 84029E RED WOOLLY HOTTIE WHITE HEART. 6 12/1/2010 8:26 3.39 17850 United Kingdom 20.34

정제 데이터 저장

retail.to_csv('./OnlineRetailClean.csv')

미니 프로젝트를 통한 데이터 분석의 목표

  1. 매출 분석
  2. 고객 분석
    • 우수고객 선별
    • 고객 리텐션 분석
  3. push notification 실행 의사 결정 하기