2 분 소요

[Pandas Crosstab ] 함수를 이용해서 빈도 테이블(Frequency Table) 을 만들어보자

먼저 데이터프레임을 준비해보자

import pandas as pd
import numpy as np
raw_data = {'name': ['수정', '수연', '민정', '우현', '재수', '우재', '수정', '수연', '지수', '유빈'], 
        'subject': ['math', 'math', 'math', 'english', 'english', 'english', 'english', 'english', 'chemistry', 'biology'], 
        'level': ['beginner', 'beginner', 'intermediate', 'intermediate', 'intermediate', 'intermediate', 'intermediate', 'intermediate','intermediate', 'intermediate'],
        'tutor': ['Ali', 'Ali', 'Ali', 'Ali', 'Ali', 'Ali', 'Jacob', 'Jacob', 'Jacob', 'Jacob'], 
        'pre_test_score': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3],
        'post_test_score': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['name', 'subject', 'level', 'tutor', 'pre_test_score', 'post_test_score'])
df

name subject level tutor pre_test_score post_test_score
0 수정 math beginner Ali 4 25
1 수연 math beginner Ali 24 94
2 민정 math intermediate Ali 31 57
3 우현 english intermediate Ali 2 62
4 재수 english intermediate Ali 3 70
5 우재 english intermediate Ali 4 25
6 수정 english intermediate Jacob 24 94
7 수연 english intermediate Jacob 31 57
8 지수 chemistry intermediate Jacob 2 62
9 유빈 biology intermediate Jacob 3 70

post_test_score 가 70점 이상이면 합격, 미만이면 불합격인 column 을 추가해주자

df['post_test_pass_fail'] = np.where(df['post_test_score'] >= 70, "Pass", "Fail")
df
name subject level tutor pre_test_score post_test_score post_test_pass_fail
0 수정 math beginner Ali 4 25 Fail
1 수연 math beginner Ali 24 94 Pass
2 민정 math intermediate Ali 31 57 Fail
3 우현 english intermediate Ali 2 62 Fail
4 재수 english intermediate Ali 3 70 Pass
5 우재 english intermediate Ali 4 25 Fail
6 수정 english intermediate Jacob 24 94 Pass
7 수연 english intermediate Jacob 31 57 Fail
8 지수 chemistry intermediate Jacob 2 62 Fail
9 유빈 biology intermediate Jacob 3 70 Pass

Tutor 기준으로 post_test_pass_fail 의 빈도를 구해보자

  • Ali 와 Jacob 이 가르치는 학생들의 시험 합격 여부를 볼 수 있다
pd.crosstab(df.tutor, df.post_test_pass_fail, margins=True)
post_test_pass_fail Fail Pass All
tutor
Ali 4 2 6
Jacob 2 2 4
All 6 4 10

Tutor 기준으로 subjectpost_test_pass_fail 빈도도 볼 수 있다

  • 튜터 기준으로 시험과목과 시험 합격 여부를 빈도표로 표현
pd.crosstab(df.tutor, [df.subject, df.post_test_pass_fail], margins=True)
subject biology chemistry english math All
post_test_pass_fail Pass Fail Fail Pass Fail Pass
tutor
Ali 0 0 2 1 2 1 6
Jacob 1 1 1 1 0 0 4
All 1 1 3 2 2 1 10

Subjectlevel 기준으로, post_test_pass_fail 빈도 확인해보자

  • 시험 과목과 레벨을 기준으로, 시험 합격 여부를 보고 싶다면 아래와 같이 표현 할 수 있다
pd.crosstab([df.subject, df.level], df.post_test_pass_fail, margins=True)
post_test_pass_fail Fail Pass All
subject level
biology intermediate 0 1 1
chemistry intermediate 1 0 1
english intermediate 3 2 5
math beginner 1 1 2
intermediate 1 0 1
All 6 4 10

crosstab 에 접근하기

  • crosstab 으로 만들어진 객체는 데이터프레임이므로, loc 으로 아래와 같이 위 테이블을 접근할 수 있다
pd.crosstab([df.subject, df.level], df.post_test_pass_fail, margins=True).loc[[('english', 'intermediate')]]
post_test_pass_fail Fail Pass All
subject level
english intermediate 3 2 5

crosstab 을 엑셀 파일로 저장해보자

  • pandas.to_excel 로 저장 가능!
pd.crosstab(df.tutor, [df.subject, df.post_test_pass_fail], margins=True).to_excel("crosstab_sample.xlsx")

댓글남기기