In [6]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:1200px !important; }</style>"))
수강 신청이 완료되었습니다. 이제 각 과목을 수강하는 학생수에 따라 크기가 다른 강의실을 배치하려고 합니다.
강의실은 규모에 따라 “Auditorium”, “Large room”, “Medium room”, “Small room” 총 4가지 종류가 있습니다.
아래 조건에 따라 강의실 종류를 지정해 주세요.
- 80명 이상의 학생이 수강하는 과목은 “Auditorium”에서 진행됩니다.
- 40명 이상, 80명 미만의 학생이 수강하는 과목은 “Large room”에서 진행됩니다.
- 15명 이상, 40명 미만의 학생이 수강하는 과목은 “Medium room”에서 진행됩니다.
- 5명 이상, 15명 미만의 학생이 수강하는 과목은 “Small room”에서 진행됩니다.
- 폐강 등의 이유로 status가 “not allowed”인 수강생은 room assignment 또한 “not assigned”가 되어야 합니다.
In [1]:
import pandas as pd
df = pd.read_csv('./enrolment_2.csv')
df.head(5)
Out[1]:
id | year | course name | status | |
---|---|---|---|---|
0 | 2777729 | 1 | science | allowed |
1 | 2777730 | 2 | science | allowed |
2 | 2777765 | 1 | arts | allowed |
3 | 2777766 | 2 | arts | allowed |
4 | 2777785 | 1 | mba | allowed |
In [2]:
df['room assignment'] = 'not assigned'
df.head(5)
Out[2]:
id | year | course name | status | room assignment | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | not assigned |
1 | 2777730 | 2 | science | allowed | not assigned |
2 | 2777765 | 1 | arts | allowed | not assigned |
3 | 2777766 | 2 | arts | allowed | not assigned |
4 | 2777785 | 1 | mba | allowed | not assigned |
In [3]:
con_allowed = df['status'] == 'allowed'
count = df.loc[con_allowed,'course name'].value_counts()
aud = list(count[count >= 80].index)
large = list(count[(count >= 40) & (count < 80)].index)
medi = list(count[(count >= 15) & (count < 40)].index)
small = list(count[(count >= 5) & (count < 15)].index)
## 이부분이 틀렸음
for room in aud:
df.loc[df["course name"] == room, "room assignment"] = 'Auditorium'
for room in large:
df.loc[df["course name"] == room, "room assignment"] = 'Large room'
for room in medi:
df.loc[df["course name"] == room, "room assignment"] = 'Medium room'
for room in small:
df.loc[df["course name"] == room, "room assignment"] = 'Small room'
df['room assignment'].value_counts()
Out[3]:
not assigned 542 Medium room 531 Small room 443 Auditorium 387 Large room 97 Name: room assignment, dtype: int64
In [4]:
import pandas as pd
df = pd.read_csv('./enrolment_2.csv')
con_allowed = df['status'] == 'allowed'
count = df.loc[con_allowed,'course name'].value_counts() #allowed 인 course name
"""
type(course_counts)으로 자료형을 확인해보면, course_counts는 series
course_counts >= 80는 각 값과 80을 비교해서, True/False를 돌려주기 때문에,
course_counts[course_counts >= 80] 는 series를 boolean인덱싱한 것
"""
# 각 강의실 규모에 해당되는 과목 리스트 만들기
aud = list(count[count >= 80].index)
large = list(count[(count >= 40) & (count < 80)].index)
medi = list(count[(count >= 15) & (count < 40)].index)
small = list(count[(count >= 5) & (count < 15)].index)
not_allowed = df["status"] == "not allowed"
df.loc[not_allowed, "room assignment"] = "not assigned"
"""
allowed 과목에 대해 값 지정해주기
course_counts는 각각의 status가 allowed인 과목들을 value count한것으로 전체의 케이스를 대변하는게 아니다
그 중에서 status가 not allowed인 행이 있을 수도 있는거에요
그래서 마지막에 status에 대한 boolean indexing을 통해 이를 걸러 준 것
"""
for room in aud:
df.loc[(df["course name"] == room) & con_allowed, "room assignment"] = 'Auditorium'
for room in large:
df.loc[(df["course name"] == room) & con_allowed, "room assignment"] = 'Large room'
for room in medi:
df.loc[(df["course name"] == room )& con_allowed, "room assignment"] = 'Medium room'
for room in small:
df.loc[(df["course name"] == room) & con_allowed, "room assignment"] = 'Small room'
df['room assignment'].value_counts()
Out[4]:
not assigned 552 Medium room 531 Small room 437 Auditorium 383 Large room 97 Name: room assignment, dtype: int64
코드잇 답안
In [5]:
import pandas as pd
df = pd.read_csv('./enrolment_2.csv')
# 과목별 인원 가져오기
allowed = df["status"] == "allowed"
course_counts = df.loc[allowed, "course name"].value_counts()
# 각 강의실 규모에 해당되는 과목 리스트 만들기
auditorium_list = list(course_counts[course_counts >= 80].index)
large_room_list = list(course_counts[(80 > course_counts) & (course_counts >= 40)].index)
medium_room_list = list(course_counts[(40 > course_counts) & (course_counts >= 15)].index)
small_room_list = list(course_counts[(15 > course_counts) & (course_counts > 4)].index)
# not allowed 과목에 대해 값 지정해주기
not_allowed = df["status"] == "not allowed"
df.loc[not_allowed, "room assignment"] = "not assigned"
# allowed 과목에 대해 값 지정해주기
for course in auditorium_list:
df.loc[(df["course name"] == course) & allowed, "room assignment"] = "Auditorium"
for course in large_room_list:
df.loc[(df["course name"] == course) & allowed, "room assignment"] = "Large room"
for course in medium_room_list:
df.loc[(df["course name"] == course) & allowed, "room assignment"] = "Medium room"
for course in small_room_list:
df.loc[(df["course name"] == course) & allowed, "room assignment"] = "Small room"
# 정답 출력
df['room assignment'].value_counts()
Out[5]:
not assigned 552 Medium room 531 Small room 437 Auditorium 383 Large room 97 Name: room assignment, dtype: int64
In [ ]:
'코드잇' 카테고리의 다른 글
[코드잇] 실리콘 밸리에는 누가 일할까?1 (0) | 2021.02.05 |
---|---|
[코드잇] 대학교 강의실 배정하기2 (0) | 2021.02.03 |
[코드잇] 대학교 수강신청 준비하기 (0) | 2021.02.03 |
[코드잇 & 머신러닝] 판다스 (0) | 2021.02.02 |
[코드잇 & 머신러닝] 넘파이 (0) | 2021.02.02 |