문제/Python

231121 Python 문제

잇꼬 2023. 11. 21. 17:46
728x90
반응형
SMALL

[문제] 입사년도별 총액 급여를 출력해주세요

emp['SALARY'].groupby(emp['HIRE_DATE'].dt.year).sum()


[문제] 입사요일별 인원수를 출력해주세요. 단, 한글요일로 출력해주세요 ('월화수목금토일'[0]+'요일')

week = emp['EMPLOYEE_ID'].groupby(emp['HIRE_DATE'].dt.dayofweek).count()
week.index 

Series(week.index).apply(lambda arg: '월화수목금토일'[arg]+'요일')

week.index = Series(week.index).apply(lambda arg: '월화수목금토일'[arg]+'요일')
week


# 건수 출력 및 정렬

week = emp['HIRE_DATE'].dt.weekday.value_counts().sort_index()
week.index = Series(week.index).apply(lambda arg: '월화수목금토일'[arg]+'요일')
week


# 부서별로 건수

emp['DEPARTMENT_ID'].value_counts()



[문제] 년도, 분기별 급여의 총액을 출력해주세요

emp['SALARY'].groupby([emp['HIRE_DATE'].dt.quarter, (emp['HIRE_DATE'].dt.year.sort_index())]).sum()


[문제] 요일별 입사 인원수에 대해서 pie chart 시각화 해주세요.

week = emp['EMPLOYEE_ID'].groupby(emp['HIRE_DATE'].dt.dayofweek).count()
week.index 
Series(week.index).apply(lambda arg: '월화수목금토일'[arg]+'요일')
week.index = Series(week.index).apply(lambda arg: '월화수목금토일'[arg]+'요일')
week

#1) 원형그래프

plt.pie(week,
        shadow=True,
        autopct='%.1f%%',
        startangle=90,
        labels=week.index)
plt.show()

 

plt.pie(week,
        shadow=True,
        autopct='%.1f%%',
        textprops={'fontsize':10, 'color':'blue'},
        wedgeprops={'width':0.7, 'edgecolor': 'yellow', 'linewidth':10},
        startangle=90,
        labels=week.index,
        counterclock=False) # 시계방향순서로 부채꼴 영역이 표시
plt.show()



[문제] 부서별 입사인원수를 막대그래프로 시각화해주세요. 단, 소속부서가 없는 사원의 인워수도 출력해주세요.

내가 쓴 답안)

plt.bar(x=x, height=y)
x = emp['EMPLOYEE_ID'].groupby(emp['DEPARTMENT_ID']).count()
y = emp['HIRE_DATE'].groupby(emp['DEPARTMENT_ID']).count()
pd.merge(emp, emp, left_on='EMPLOYEE_ID', right_on='DEPARTMENT_ID', how='left').count()

pd.merge(emp, emp, left_on='MANAGER_ID', right_on='EMPLOYEE_ID', how='left')[['LAST_NAME_x', 'LAST_NAME_y']]
plt.bar(x=x, height=y)
plt.title='부서별 입사인원수'
plt.xlabel('입사인원수')
plt.ylabel('부서명')
plt.show()

 

강사님 답안)

emp.DEPARTMENT_ID.value_counts()
dept_cnt = emp.DEPARTMENT_ID.fillna(999).value_counts()
dept_cnt.sort_index(inplace=True)
dept_cnt = dept_cnt.reset_index()
dept_cnt.columns = ['부서', '도수']
dept_cnt['부서'] = dept_cnt['부서'].astype(int)
dept_cnt[dept_cnt['부서'] == 999]

dept_cnt.loc[dept_cnt['부서'] == 999, '부서'] = '부서X'
dept_cnt


# 수직형 막대그래프

plt.bar(x=dept_cnt.index, height=dept_cnt['도수'])
plt.xticks(dept_cnt.index, dept_cnt['부서'],fontsize=10, color='blue', rotation=45)
plt.yticks(range(0,55,5))
title_font = {"fontsize":20, "fontweight":"bold"}
plt.title('부서 인원수 현황', color='blue', loc='right', pad=30)
plt.show()

dept_cnt.도수.plot(kind='bar')
plt.xticks(dept_cnt.index, dept_cnt['부서'],  # x축 눈금명 수정
           fontsize=10, color='blue', rotation=45)
plt.yticks(range(0,55,5))
title_font = {"fontsize":20, "fontweight":"bold"}
plt.title('부서 인원수 현황', fontdict=title_font, color='blue', loc='right', pad=30)
plt.show()


# 수평형 막대그래프

plt.barh(y=dept_cnt.index, width=dept_cnt['도수'])
plt.yticks(dept_cnt.index, dept_cnt['부서'], fontsize=7,color= 'blue', rotation=45) 
plt.xticks(range (0,55,5))
title_font = {"fontsize":20, "fontweight":"bold"}
plt.title("부서 인원수 현황", fontdict=title_font ,size = 15, color = 'darkblue', loc = 'right', pad=30)
plt.show()

dept_cnt.도수.plot(kind='barh')
plt.yticks(dept_cnt.index, dept_cnt['부서'],fontsize=7,color='blue', rotation=45)
plt.xticks(range(0,55,5))
title_font = {"fontsize":20, "fontweight":"bold"}
plt.title("부서 인원수 현황", size = 15, color = 'darkblue', loc = 'right', pad=30)
plt.show()


[문제] 분기별 입사 인원수를 원형차트, 막대그래프를 시각화해주세요

emp_quarter = emp.HIRE_DATE.dt.quarter.value_counts()
quarter = emp.HIRE_DATE.dt.quarter.value_counts()


#1) 원형차트 

plt.pie(emp_quarter,
        shadow=True,
        startangle=90,
        autopct='%.1f%%',   
        explode=(0.1, 0.0, 0.0, 0.0))
plt.legend(labels=emp_quarter.index, loc='upper right')
plt.title('분기별 입사 인원수',size=30)


## 강사님 답변

plt.pie(quarter, autopct='%1.1f%%')
plt.title('분기별 입사 현황', fontsize=10)
plt.legend(labels = [str(i) + '분기' for i in quarter.index], loc='lower center', ncol=4)
plt.show()


#2) 막대그래프(수직형)

plt.bar(x=emp_quarter.index, height=emp_quarter)
title_font = {"fontsize":20, "fontweight":"bold"}
plt.title('분기별 입사 인원수', fontdict=title_font)
plt.show()


## 강사님 답변

plt.bar(x = quarter.index, height=quarter)
plt.xticks(ticks=quarter.index, labels=[str(i) + '분기' for i in quarter.index])
for i in quarter.index:
    plt.text(i-0.1, # x축
             quarter[i]+0.3, # y축
             quarter[i])
plt.show()


#3) 막대그래프(수평)

plt.barh(y = quarter.index, width=quarter)
plt.yticks(ticks=quarter.index, labels=[str(i) + '분기' for i in quarter.index])
for i in quarter.index:
    plt.text(quarter[i]+0.3, # x축
             i-0.1, # y축
             quarter[i])
plt.show()
728x90
반응형
LIST

'문제 > Python' 카테고리의 다른 글

231123 Python 문제  (1) 2023.11.23
231122 Python 문제  (0) 2023.11.22
231120 Python 문제  (0) 2023.11.20
231120 Python 문제  (1) 2023.11.20
231116 Python 문제  (0) 2023.11.20