목표¶
- 아이템별 지표 확인하기
- 시간별 지역별 판매 지표 확인하기
In [1]:
import numpy as np
import pandas as pd
# seaborn
import seaborn as sns
COLORS = sns.color_palette()
%matplotlib inline
데이터 로딩¶
- 정제된 데이터 사용(retail.csv)
In [2]:
dtypes = {
'UnitPrice': np.float32,
'CustomerID': np.int32,
'Quantity': np.int32
}
retail = pd.read_csv('./OnlineRetailClean.csv', dtype=dtypes)
retail.head()
Out[2]:
날짜 타입 데이터 변환¶
- 문자열로 로딩하는 것보다 date/datetime 타입으로 로딩하는 것이 분석에 용이
In [3]:
retail.info()
In [4]:
retail['InvoiceDate']=pd.to_datetime(retail['InvoiceDate'],infer_datetime_format=True)
retail.info()
해당 기간 동안의 매출¶
- 전체 매출
- 국가별 매출
- 월별 매출
- 요일별 매출
- 시간별 매출
전체 매출¶
In [5]:
retail['CheckoutPrice'].sum()
Out[5]:
국가별 매출¶
In [6]:
retail.groupby('Country').sum()['CheckoutPrice'].sort_values()
Out[6]:
월별 매출¶
In [7]:
retail.head()
Out[7]:
In [8]:
retail.set_index('InvoiceDate')
Out[8]:
In [9]:
#def extract_month(date):
# month = str(date.month)
# if date.month <10:
# month='0'+month
# return str(date.year)+month
def extract_month(date):
return str(date.year)+str(date.month).rjust(2,'0')
# rjust사용하기 위해서는 string으로 변환 먼저 해줘야 함
In [10]:
#grouping하기 위해서 해당 변수를 우선 index로 만들기
rev_by_month=retail.set_index('InvoiceDate').groupby(extract_month).sum()['CheckoutPrice']
rev_by_month
Out[10]:
In [11]:
def plot_bar(df, xlabel,ylabel,title,color=COLORS[0], figsize=(30,10), rotation=45):
plot=df.plot(kind='bar')
plot.set_xlabel(xlabel,fontsize=11)
plot.set_ylabel(ylabel,fontsize=11)
plot.set_title(title, fontsize=13)
plot.set_xticklabels(labels=df.index, rotation=rotation)
plot_bar(rev_by_month,'Month','Revenue','Revenue by Month')
요일별 매출¶
In [12]:
rev_by_dow=retail.set_index('InvoiceDate').groupby(lambda date:date.dayofweek).sum()['CheckoutPrice']
rev_by_dow #0이 월요일, 5가 토요일, 6이 일요일
Out[12]:
In [13]:
plot_bar(rev_by_dow,'DOW','Revenue','Revenue by Day of Week')
In [14]:
Day_week=np.array(['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])
Day_week
Out[14]:
In [15]:
rev_by_dow.index=Day_week[rev_by_dow.index]
plot_bar(rev_by_dow,'DOW','Revenue','Revenue by Day of Week')
시간별 매출¶
In [16]:
rev_by_hour=retail.set_index('InvoiceDate').groupby(lambda date:date.hour).sum()['CheckoutPrice']
plot_bar(rev_by_hour,'Hour','Revenue','Revenue by Hour')
매출 데이터로부터 insight¶
- 전체 매출의 82%가 UK에서 발생
- 11년도의 가장 많은 주문이 발생한 달 11월(12월의 전체 데이터가 반영이 되진 않았음)
- 11, 12월의 판매량이 압도(블랙프라이데이, 사이버먼데이, 크리스마스 휴일)
- 일주일중 목요일까지는 성장세를 보이다가, 이후로 하락(토요일에는 주문X)
- 7시를 시작으로 주문이 시작되어 12시까지 증가세, 15시까지 하락을, 15시 이후 부터 급락)
제품별 metrics¶
- Top 10 판매 제품
- Top 10 매출 제품
In [17]:
#Top 3 판매 제품
top_selling=retail.groupby('StockCode').sum()['Quantity'].sort_values(ascending=False)[:3]
top_selling
Out[17]:
In [18]:
#Top 10 매출 제품
top_revenue=retail.groupby('StockCode').sum()['CheckoutPrice'].sort_values(ascending=False)[:10]
top_revenue
Out[18]:
top 3 아이템의 월별 판매량 추이¶
In [19]:
#전체 아이템에 대한 월별 판매량 추이
retail.set_index('InvoiceDate').groupby(['StockCode',extract_month]).sum()[['Quantity','CheckoutPrice']]
Out[19]:
In [20]:
#top3 아이템에 대한 월별 판매량 추이
monthly_top3=retail.set_index('InvoiceDate').groupby(['StockCode',extract_month]).sum()[['Quantity','CheckoutPrice']].loc[top_selling.index]
monthly_top3
Out[20]:
In [21]:
plot_bar(monthly_top3['CheckoutPrice'],'Product/Month','Revenue','top3 items')
In [ ]:
'머신러닝' 카테고리의 다른 글
파이썬_딥러닝 시작하기에 앞서 (오류메시지 안뜨는 keras, tensorflow설치 방법) (0) | 2020.09.25 |
---|---|
파이썬_캐글(kaggle) 타이타닉 생존 예측 (점수 77% 달성 후기) (0) | 2020.09.21 |
파이썬_쇼핑몰 매출 분석1(판매 데이터 전처리, 전체매출, 지역별 매출 분석) (0) | 2020.09.20 |
파이썬_쇼핑몰 고객 주문 데이터 분석 (판매 데이터 파악, 데이터 정제) (2) | 2020.09.20 |
파이썬_캐글(kaggle) 로지스틱 회귀분석(logistic regression) 활용한 타이타닉 생존 예측 (0) | 2020.09.20 |