{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mission 2\n", "\n", "- 첨부된 데이터는 다이어트 식품을 판매하는 어플리케이션 회사에서 나온 고객 결제 데이터 입니다. 고객들이 온라인 마케팅으로 유입된 채널에 따라, 결제를 완료한 고객도 있고, 유입이 되었으나 이탈한 고객도 있습니다. 이 데이터로 아래의 미션을 수행보세요. \n", "- 먼저 개인적으로 풀어본 다음, 팀별로 모여 문제를 풀어보세요.\n", "- Chat GPT와 책, 유튜브 등 여러 자료를 활용하여 최대한 풀어보세요. \n", "- 기한 : 2023년 11/30 23:59 까지\n", "- 제출처 : woghkszhf@naver.com" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. 02_Data.csv파일을 불러와, df1으로 선언하고 데이터의 구조와 타입을 확인하시오." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
회원코드프로그램상태프로그램가격최종결제금액결제시각결제유형채널보조식품가격보조식품(추가구매)
0Y9RY2VSI체중감량 프로그램 (타입B)completed1125001125002017-04-14 오후 7:03:29.976000Recurringothers0구매안함
13GTN3S3B체중감량 프로그램 (타입B)completed44780447802017-05-23 오후 8:53:54.368000Recurringothers0구매안함
26B0IG276체중감량 프로그램 (타입 A)completed1320001490002017-08-23 오후 11:39:21.840000Recurringfacebook17000식이요법보조제A
3EMGRU2MO체중감량 프로그램 (타입 A)completed1125001125002017-08-28 오후 8:18:22.824000Recurringnaver0구매안함
41ELG96TX체중감량 프로그램 (타입B)completed44780447802017-05-07 오후 5:50:30.944000Recurringfacebook0구매안함
\n", "
" ], "text/plain": [ " 회원코드 프로그램 상태 프로그램가격 최종결제금액 \\\n", "0 Y9RY2VSI 체중감량 프로그램 (타입B) completed 112500 112500 \n", "1 3GTN3S3B 체중감량 프로그램 (타입B) completed 44780 44780 \n", "2 6B0IG276 체중감량 프로그램 (타입 A) completed 132000 149000 \n", "3 EMGRU2MO 체중감량 프로그램 (타입 A) completed 112500 112500 \n", "4 1ELG96TX 체중감량 프로그램 (타입B) completed 44780 44780 \n", "\n", " 결제시각 결제유형 채널 보조식품가격 보조식품(추가구매) \n", "0 2017-04-14 오후 7:03:29.976000 Recurring others 0 구매안함 \n", "1 2017-05-23 오후 8:53:54.368000 Recurring others 0 구매안함 \n", "2 2017-08-23 오후 11:39:21.840000 Recurring facebook 17000 식이요법보조제A \n", "3 2017-08-28 오후 8:18:22.824000 Recurring naver 0 구매안함 \n", "4 2017-05-07 오후 5:50:30.944000 Recurring facebook 0 구매안함 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "df1=pd.read_csv('미션2_데이터.csv')\n", "df1.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 10)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1.shape" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "회원코드 object\n", "프로그램 object\n", "상태 object\n", "프로그램가격 int64\n", "최종결제금액 int64\n", "결제시각 object\n", "결제유형 object\n", "채널 object\n", "보조식품가격 int64\n", "보조식품(추가구매) object\n", "dtype: object" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1.dtypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. '프로그램가격'의 평균, 최대값, 최소값을 확인하시오." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "107778.024\n", "177000\n", "44780\n" ] } ], "source": [ "print(df1['프로그램가격'].mean())\n", "print(df1['프로그램가격'].max())\n", "print(df1['프로그램가격'].min())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. '채널'의 항목과 빈도수를 확인하고, '채널'별 '최종결제금액'의 합을 계산하여, result1.xlsx로 저장하시오." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "채널의 항목과 빈도수\n", "\n", " facebook 6880\n", "others 1390\n", "naver 1009\n", "direct 297\n", "email 271\n", "google 120\n", "instagram 33\n", "Name: 채널, dtype: int64 \n", "\n", "채널별 최종결제금액의 합\n", "\n", "채널\n", "direct 31337160\n", "email 30655080\n", "facebook 825136460\n", "google 12663480\n", "instagram 3562620\n", "naver 111491750\n", "others 149349690\n", "Name: 최종결제금액, dtype: int64\n" ] } ], "source": [ "print('채널의 항목과 빈도수\\n\\n',df1['채널'].value_counts(),'\\n')\n", "result=df1.groupby('채널')['최종결제금액'].sum()\n", "print(f'채널별 최종결제금액의 합\\n\\n{result}')\n", "result.to_excel('result1.xlsx',index=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. '채널'의 빈도수가 높은 상위 3개 항목을 뽑아, 해당 데이터의 '보조식품(추가구매)'에 따른 '최종결제금액'의 합과 평균을 계산하시오. " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "구매안함 7520\n", "식이요법보조제A 1582\n", "식이요법보조제B 388\n", "단백질보충제A 340\n", "종합비타민A 108\n", "단백질보충제B 62\n", "Name: 보조식품(추가구매), dtype: int64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1['보조식품(추가구매)'].value_counts()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "보조식품(추가구매)\n", "구매안함 844581880\n", "단백질보충제A 43582030\n", "단백질보충제B 6962620\n", "식이요법보조제A 199833090\n", "식이요법보조제B 54792540\n", "종합비타민A 14444080\n", "Name: 최종결제금액, dtype: int64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "top3=df1['채널'].value_counts()[:3]\n", "top3\n", "df1.groupby('보조식품(추가구매)')['최종결제금액'].sum()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
freq
채널
facebook6880
others1390
naver1009
\n", "
" ], "text/plain": [ " freq\n", "채널 \n", "facebook 6880\n", "others 1390\n", "naver 1009" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1.groupby('채널').agg(freq=('채널','count')).sort_values('freq',ascending=False).head(3)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
summean
보조식품(추가구매)
구매안함770197460112766.831625
단백질보충제A41403610128983.208723
단백질보충제B6962620112300.322581
식이요법보조제A199833090126316.744627
식이요법보조제B54792540141217.886598
종합비타민A12788580133214.375000
\n", "
" ], "text/plain": [ " sum mean\n", "보조식품(추가구매) \n", "구매안함 770197460 112766.831625\n", "단백질보충제A 41403610 128983.208723\n", "단백질보충제B 6962620 112300.322581\n", "식이요법보조제A 199833090 126316.744627\n", "식이요법보조제B 54792540 141217.886598\n", "종합비타민A 12788580 133214.375000" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1.query(\"채널 in ['facebook','others','naver']\").groupby('보조식품(추가구매)').agg(sum = ('최종결제금액','sum'),\n", " mean = ('최종결제금액','mean'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. '결제시각'데이터에서 날짜와 시간데이터를 나누어 데이터만 뽑아, '결제날짜'와 '결제시간'파생변수로 각각 선언하시오. " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
회원코드프로그램상태프로그램가격최종결제금액결제시각결제유형채널보조식품가격보조식품(추가구매)결제날짜결제시간
0Y9RY2VSI체중감량 프로그램 (타입B)completed1125001125002017-04-14 오후 7:03:29.976000Recurringothers0구매안함2017-04-1407:03:29.976000
13GTN3S3B체중감량 프로그램 (타입B)completed44780447802017-05-23 오후 8:53:54.368000Recurringothers0구매안함2017-05-2308:53:54.368000
26B0IG276체중감량 프로그램 (타입 A)completed1320001490002017-08-23 오후 11:39:21.840000Recurringfacebook17000식이요법보조제A2017-08-2311:39:21.840000
\n", "
" ], "text/plain": [ " 회원코드 프로그램 상태 프로그램가격 최종결제금액 \\\n", "0 Y9RY2VSI 체중감량 프로그램 (타입B) completed 112500 112500 \n", "1 3GTN3S3B 체중감량 프로그램 (타입B) completed 44780 44780 \n", "2 6B0IG276 체중감량 프로그램 (타입 A) completed 132000 149000 \n", "\n", " 결제시각 결제유형 채널 보조식품가격 보조식품(추가구매) \\\n", "0 2017-04-14 오후 7:03:29.976000 Recurring others 0 구매안함 \n", "1 2017-05-23 오후 8:53:54.368000 Recurring others 0 구매안함 \n", "2 2017-08-23 오후 11:39:21.840000 Recurring facebook 17000 식이요법보조제A \n", "\n", " 결제날짜 결제시간 \n", "0 2017-04-14 07:03:29.976000 \n", "1 2017-05-23 08:53:54.368000 \n", "2 2017-08-23 11:39:21.840000 " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from datetime import datetime\n", "import re\n", "\n", "df1['결제시각1'] = df1['결제시각'].str.replace('[가-힣]', '', regex=True) #정규 표현식 \n", "df1.head(3)\n", "\n", "df1['결제시각1'] = pd.to_datetime(df1['결제시각1'])\n", "\n", "df1['결제날짜'] = df1['결제시각1'].dt.date\n", "df1['결제시간'] = df1['결제시각1'].dt.time\n", "df1.head()\n", "\n", "df1 = df1.drop('결제시각1', axis=1)\n", "df1.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. 앞서 추출된 '결제날짜'데이터를 이용하여, '연도' / '월' / '일' 데이터를 추출하시오." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
회원코드프로그램상태프로그램가격최종결제금액결제시각결제유형채널보조식품가격보조식품(추가구매)결제날짜결제시간연도
0Y9RY2VSI체중감량 프로그램 (타입B)completed1125001125002017-04-14 오후 7:03:29.976000Recurringothers0구매안함2017-04-1407:03:29.976000201744
13GTN3S3B체중감량 프로그램 (타입B)completed44780447802017-05-23 오후 8:53:54.368000Recurringothers0구매안함2017-05-2308:53:54.368000201755
26B0IG276체중감량 프로그램 (타입 A)completed1320001490002017-08-23 오후 11:39:21.840000Recurringfacebook17000식이요법보조제A2017-08-2311:39:21.840000201788
\n", "
" ], "text/plain": [ " 회원코드 프로그램 상태 프로그램가격 최종결제금액 \\\n", "0 Y9RY2VSI 체중감량 프로그램 (타입B) completed 112500 112500 \n", "1 3GTN3S3B 체중감량 프로그램 (타입B) completed 44780 44780 \n", "2 6B0IG276 체중감량 프로그램 (타입 A) completed 132000 149000 \n", "\n", " 결제시각 결제유형 채널 보조식품가격 보조식품(추가구매) \\\n", "0 2017-04-14 오후 7:03:29.976000 Recurring others 0 구매안함 \n", "1 2017-05-23 오후 8:53:54.368000 Recurring others 0 구매안함 \n", "2 2017-08-23 오후 11:39:21.840000 Recurring facebook 17000 식이요법보조제A \n", "\n", " 결제날짜 결제시간 연도 월 일 \n", "0 2017-04-14 07:03:29.976000 2017 4 4 \n", "1 2017-05-23 08:53:54.368000 2017 5 5 \n", "2 2017-08-23 11:39:21.840000 2017 8 8 " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "com_date=df1['결제날짜']\n", "sep_date=pd.to_datetime(com_date)\n", "df1['연도']=sep_date.dt.year\n", "df1['월']=sep_date.dt.month\n", "df1['일']=sep_date.dt.month\n", "df1.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7. 결제 '월'별 '최종결제금액'의 합을 계산하시오." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "결제 \"월\"별 최종결제금액 : \n", "\n", " 월\n", "3 67579200\n", "4 133340640\n", "5 185577300\n", "6 203945100\n", "7 178364700\n", "8 395389300\n", "Name: 최종결제금액, dtype: int64\n" ] } ], "source": [ "print('결제 \"월\"별 최종결제금액 :','\\n\\n', df1.groupby('월')['최종결제금액'].sum())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8. '최종결제금액'이 높은 30명의 고객 명단을 추출하여, result2.xlsx로 선언하시오." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
회원코드최종결제금액
416535OZZBQ9210000
8057D8V97HTG194000
8220FY0Y87PN194000
4402RU2MHVDS194000
3328TTFOV4HW194000
3660E6CNFZUN194000
3950QHLHEWLK194000
7808UPGM7P9G194000
273627QV33L6194000
3471CR33FGED194000
7911GRYDUHGT194000
2581SPBGROKC194000
4928JDH0HI93194000
65253B91KE6E194000
4947GTA7TYEC194000
13086P4UG7M5194000
3111YOPSJCNI194000
4006FRM4EROO194000
86837V7E8NL3185750
4382ONG38KK6177000
7840458433H5177000
5145G1EFPB2Q177000
93731F1Y479Z177000
1498QUCDJ17Y177000
38422D85G74177000
83036KSV5ZJS177000
1617DF8MN951168750
5426FRKM9IBG165000
468062I4OHDV165000
6186GMJ1BVGE165000
\n", "
" ], "text/plain": [ " 회원코드 최종결제금액\n", "4165 35OZZBQ9 210000\n", "8057 D8V97HTG 194000\n", "8220 FY0Y87PN 194000\n", "4402 RU2MHVDS 194000\n", "3328 TTFOV4HW 194000\n", "3660 E6CNFZUN 194000\n", "3950 QHLHEWLK 194000\n", "7808 UPGM7P9G 194000\n", "2736 27QV33L6 194000\n", "3471 CR33FGED 194000\n", "7911 GRYDUHGT 194000\n", "2581 SPBGROKC 194000\n", "4928 JDH0HI93 194000\n", "6525 3B91KE6E 194000\n", "4947 GTA7TYEC 194000\n", "1308 6P4UG7M5 194000\n", "3111 YOPSJCNI 194000\n", "4006 FRM4EROO 194000\n", "8683 7V7E8NL3 185750\n", "4382 ONG38KK6 177000\n", "7840 458433H5 177000\n", "5145 G1EFPB2Q 177000\n", "9373 1F1Y479Z 177000\n", "1498 QUCDJ17Y 177000\n", "384 22D85G74 177000\n", "8303 6KSV5ZJS 177000\n", "1617 DF8MN951 168750\n", "5426 FRKM9IBG 165000\n", "4680 62I4OHDV 165000\n", "6186 GMJ1BVGE 165000" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vip=df1[['회원코드','최종결제금액']].sort_values('최종결제금액',ascending=False).head(30)\n", "vip.to_excel('vip30.xlsx',index=False)\n", "vip" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9. '상태'가 completed에 해당하는 데이터만 뽑아, '채널'별 '최종결제금액'의 합을 계산하시오. " ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "completed 5400\n", "cancelled 4010\n", "refunded 590\n", "Name: 상태, dtype: int64" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1['상태'].value_counts()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
최종결제금액합계
채널
direct17538760
email17475380
facebook437954060
google7017240
instagram1890780
naver63103330
others83813430
\n", "
" ], "text/plain": [ " 최종결제금액합계\n", "채널 \n", "direct 17538760\n", "email 17475380\n", "facebook 437954060\n", "google 7017240\n", "instagram 1890780\n", "naver 63103330\n", "others 83813430" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "comp_situ=df1[df1['상태']=='completed']\n", "comp_situ.groupby('채널').agg(최종결제금액합계=('최종결제금액','sum'))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }