오늘 한 일
- 데이터 분석 종합반 5주차 강의 (5-1 ~ 5-10)
5-10 [과제]
더보기
#라이브러리 불러오기
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rc('font', family='NanumBarunGothic')
# 한글깨짐 방지
plt.rc('font', family='NanumBarunGothic')
sparta_data = pd.read_table('/content/cohort_data.csv',sep=',')
sparta_data.tail()
#날짜 데이터 타입 변경
format='%Y-%m-%dT%H:%M:%S.%f'
sparta_data['start_time'] = pd.to_datetime(sparta_data['created_at'], errors='coerce')
sparta_data.tail()
#시작 week 구하기
sparta_data['start_week']= sparta_data['start_time'].dt.isocalendar().week
sparta_data.tail()
#시작 주 범위 알기
category_range = set(sparta_data['start_week'])
category_range
# 범주화 하기
#번주화할 데이터
progress_rate = list(sparta_data['progress_rate'])
progress_rate
#범주를 구분하는 기준 bins
bins = [0,4.11,26.03,41.10,61.64,80.82,100]
#구분한 범주의 라벨 labels
labes=[0,1,2,3,4,5]
#범주화에 사용하는 함수 pd.cut
cuts = pd.cut(progress_rate,bins, right=True,include_lowest=True, labels=labes)
cuts
cuts = pd.DataFrame(cuts)
cuts.tail()
# 표 합치기
sparta_data = pd.concat([sparta_data,cuts],axis=1, join='inner')
sparta_data.head()
#표 인덱스 변경하기
sparta_data.columns=['created_at','user_id','name','progress_rate','start_time','start_week',"week"]
sparta_data.head()
#시작주와, 수강 주차별 기준으로 표 grouping 하기
grouping = sparta_data.groupby(['start_week','week'])
grouping.head()
#시작주와, 수강 주차별에 해당하는 수강생 수 구하기
cohort_data = grouping['user_id'].apply(pd.Series.nunique)
cohort_data = pd.DataFrame(cohort_data)
cohort_data.head(10)
#각 주차별 수강한 수강생 총 합 구하기
k=31
for i in range(6):
for j in range(5, 0, -1):
cohort_data.at[(k,j-1), 'user_id'] = int(cohort_data.at[(k,j),'user_id']) + int(cohort_data.at[(k,j-1),'user_id'])
k=k+1
cohort_data = cohort_data.reset_index()
cohort_data.head()
cohort_counts = cohort_data.pivot(index="start_week",
columns="week",
values="user_id")
cohort_counts
# 앞서 만든 피벗 테이블을 retention 변수에 저장하기
retention = cohort_counts
#각 주(week) 별 최초 수강생 수만 가져오기
cohort_sizes = cohort_counts.iloc[:,0]
cohort_sizes.head()
# 최초 수강생 수를 각 데이터에 나눠주기
retention = cohort_counts.divide(cohort_sizes, axis=0)
retention
#각 수치 퍼센트로 변경하기
retention.round(3)*100
#주차별 수강 전환율 구하기
w=31
for i in range(6):
for j in range(1, 6):
if retention.at[(w, j - 1)] != 0:
retention.at[(2, j)] = retention.at[(w, j)] / retention.at[(w, j- 1)]
w=w+1
retention
#주차별 수강 전환율 히트맵
plt.figure(figsize=(10,8))
sns.heatmap(data=retention,
annot=True,
fmt='.0%',
vmin=0,
vmax=1,
cmap="BuGn")
plt.title('개강일별 주차 간 전환율', fontsize=20)
plt.xlabel('주차', fontsize=14,labelpad=30)
plt.ylabel('개강일', fontsize=14,rotation=360,labelpad=30)
plt.yticks(rotation=360)
plt.show()
- 커리어 스터디
발표자료 만들기
더보기