데이터분석
(5) 데이터분석_가상 쇼핑몰 데이터 프로젝트
빠스무
2023. 6. 13. 11:30
728x90
가상 쇼핑몰 데이터를 분석해보자
pandas 사용을 위해 import를 시켜준다
import pandas as pd
쇼핑몰 데이터를 경로에따라서 읽어온다
retail = pd.read_csv('/content/drive/MyDrive/study/데이터분석/OnlineRetail.csv')

각 컬럼에 대한 설명
- InvoiceNo: 주문번호
- StockCode: 상품코드
- Description: 상품설명
- Quantity: 주문수량
- InvoiceDate: 주문날짜
- UnitPrice: 상품가격
- CustomerID: 고객아이디
- Country: 고객거주지역(국가)
pd.options.display.max_rows = 10
retail.isnull().sum()

# 각 필드당 null이 몇 % 있는지 확인
retail.isnull().mean()

# 비회원을 제거
retail = retail[pd.notnull(retail['CustomerID'])]

#구입 수량이 0이하인 데이터 확인
retail[retail['Quantity'] <= 0]

# 구입 가격이 0이하인 데이터 확인
retail[retail['UnitPrice'] <= 0]

# 고객의 총 비출비용 (CheckoutPrice) 파생변수 구하기
# 수량 * 가격
retail['CheckoutPrice'] = retail['UnitPrice'] * retail['Quantity']
retail

retail.info()

retail['InvoiceDate'] = pd.to_datetime(retail['InvoiceDate'])

total_revenue = retail['CheckoutPrice'].sum()
total_revenue

retail['Country'].value_counts()

국가별 매출
rev_by_countries = retail.groupby('Country')['CheckoutPrice'].sum().sort_values()
rev_by_countries

plot = rev_by_countries.plot(kind='bar', figsize=(20,10))
plot.set_xlabel('Country', fontsize = 12)
plot.set_ylabel('Revenue', fontsize = 12)
plot.set_title('Revenue by Country', fontsize = 15)
plot.set_xticklabels(labels = rev_by_countries.index,rotation = 45)

# 월별 매출 구하기
retail['InvoiceDate'].sort_values(ascending=False)

# 2011 - 03 - 01 입력
def extract_month(date):
month = str(date.month) # 3
if date.month < 10:
month = '0' + month # 03
return str(date.year) + month # 201103
rev_by_month = retail.set_index('InvoiceDate').groupby(extract_month)['CheckoutPrice'].sum()

def plot_bar(df, xlabel, ylabel,title, rotation = 45, titlesize = 15,fontsize = 12, figsize=(20,10) ):
plot = df.plot(kind='bar', figsize=figsize)
plot.set_xlabel(xlabel, fontsize = fontsize)
plot.set_ylabel(ylabel, fontsize= fontsize )
plot.set_title(title, fontsize = fontsize )
plot.set_xticklabels(labels = df.index,rotation = rotation)
plot_bar(rev_by_month, 'Month', 'Revenue', 'Revenue by Month')

# 요일별 매출 구하기
def extract_dow(date):
return date.dayofweek
rev_by_dow = retail.set_index('InvoiceDate').groupby(lambda date: date.dayofweek)['CheckoutPrice'].sum()


plot_bar(rev_by_dow,'DOW','Revenue','Revenue by DOM')

# 시간대별 매출 구하기
rev_by_hour = retail.set_index('InvoiceDate').groupby(lambda date: date.hour)['CheckoutPrice'].sum()
plot_bar(rev_by_hour,'hour','Revenue','Revenue by DOM')

# 판매제품(StockCode) Top 10
# 기준: Quantity
rev_by_StockCode= retail.groupby('StockCode')['Quantity'].sum().sort_values(ascending = False)[0:10]
plot_bar(rev_by_StockCode, 'Month', 'Revenue', 'Revenue by Month')

# 우수고객(Customer Id) Top 10
# 지불금액: CheckoutPrice
rev_by_CustomerID= retail.groupby('CustomerID')['CheckoutPrice'].sum().sort_values(ascending = False).head(10)
plot_bar(rev_by_CustomerID, 'Month', 'Revenue', 'Revenue by Month')


데이터로부터 Insight
- 전체 매출의 약 82%가 UK에서 발생
- 11년도에 가장 많은 매출이 발생한 달은 11월
- 매출은 꾸준히 성장(11년 12월 데이터는 9일까지만 포함)
- 토요일은 영업을 하지 않음
- 새벽 6시에 오픈, 오후 9시에 마감이 예상
- 일주일 중 목요일까지는 성장세를 보이고,이후 하락