2025 내일배움캠프 QAQC_2기

2025 내일배움캠프 QAQC_2기 본캠프 29일차

mingdoremi 2025. 6. 23. 21:51

안녕하세요! 오늘은 본캠프 29일차입니다!!

월요일이네요..! 다들 잘 쉬셨나요~ 

오늘의 베이직반 기초 문제!

 

1. 도시별 평균 나이

다음 DataFrame에서 'City'를 기준으로 그룹화하여 각 도시의 'Age' 평균을 계산하고 출력하세요.

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'], 'City': ['Seoul', 'Busan', 'Seoul', 'Jeju', 'Busan'], 'Age': [25, 30, 28, 22, 35]}

#다음 DataFrame에서 'City'를 기준으로 그룹화하여 각 도시의 'Age' 평균을 계산하고 출력하세요.
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'], 'City': ['Seoul', 'Busan', 'Seoul', 'Jeju', 'Busan'], 'Age': [25, 30, 28, 22, 35]}
df = pd.DataFrame(data)

city_age_mean = df.groupby("City")['Age'].mean()
print(city_age_mean)

 

2. 제품별 총 판매량

다음 DataFrame에서 'Product'를 기준으로 그룹화하여 각 제품의 'Sales' 합계를 계산하고 출력하세요.

import pandas as pd data = {'Product': ['Laptop', 'Mouse', 'Keyboard', 'Laptop', 'Mouse'], 'Sales': [100, 50, 70, 120, 60]}

#다음 DataFrame에서 'Product'를 기준으로 그룹화하여 각 제품의 'Sales' 합계를 계산하고 출력하세요.
import pandas as pd

data = {'Product': ['Laptop', 'Mouse', 'Keyboard', 'Laptop', 'Mouse'], 'Sales': [100, 50, 70, 120, 60]}
df = pd.DataFrame(data)
product_sale_sum = df.groupby('Product')['Sales'].sum()
print(product_sale_sum)

 

3. 부서별 최소/최대 급여

다음 DataFrame에서 'Department'를 기준으로 그룹화하여 각 부서의 'Salary'에 대한 최솟값과 최댓값을 동시에 계산하고 출력하세요.

data = {'Employee': ['A', 'B', 'C', 'D'], 'Department': ['HR', 'IT', 'HR', 'IT'], 'Salary': [50000, 70000, 55000, 80000]}

#다음 DataFrame에서 'Department'를 기준으로 그룹화하여 각 부서의 'Salary'에 대한 최솟값과 최댓값을 동시에 계산하고 출력하세요.
import pandas as pd

data = {'Employee': ['A', 'B', 'C', 'D'], 'Department': ['HR', 'IT', 'HR', 'IT'], 'Salary': [50000, 70000, 55000, 80000]}
df = pd.DataFrame(data)
salary_min_max = df.groupby('Department')['Salary'].agg(['min', 'max'])
print(salary_min_max)

 

4. 점수 등급 범주화

다음 학생들의 시험 점수를 0-60 미만: 'F', 60-70 미만: 'D', 70-80 미만: 'C', 80-90 미만: 'B', 90-100: 'A'로 범주화하여 새로운 'Grade' 컬럼을 추가하고 출력하세요.

scores = pd.Series([85, 92, 78, 65, 95, 70, 55, 88])

import pandas as pd

scores = pd.Series([85, 92, 78, 65, 95, 70, 55, 88])

grade_list = []

for score in scores:
    if score < 60:
        grade_list.append('F')
    elif score < 70:
        grade_list.append('D')
    elif score < 80:
        grade_list.append('C')
    elif score < 90:
        grade_list.append('B')
    else:
        grade_list.append('A')

grades = pd.Series(grade_list, dtype = pd.CategoricalDtype(categories=['F', 'D', 'C', 'B', 'A'], ordered=True))
print(grades)

 

5. 나이대별 그룹화

다음 사람들의 나이를 10대, 20대, 30대, 40대 이상으로 그룹화하여 새로운 'Age_Group' 컬럼을 추가하고, 각 나이 그룹별 인원수를 출력하세요. ages = pd.Series([15, 22, 31, 45, 18, 28, 37, 42])

풀어보기

#다음 사람들의 나이를 10대, 20대, 30대, 40대 이상으로 그룹화하여 새로운 'Age_Group' 컬럼을 추가하고, 각 나이 그룹별 인원수를 출력하세요. 

import pandas as pd

ages = pd.Series([15, 22, 31, 45, 18, 28, 37, 42])

age_groups = []

for age in ages:
    if age < 20:
        age_groups.append('10대')
    elif age < 30:
        age_groups.append('20대')
    elif age < 40:
        age_groups.append('30대')
    else:
        age_groups.append('40대 이상')

age_group_series = pd.Series(age_groups)

print(age_group_series.value_counts().sort_index())

 

6. 수입 구간 분류

다음 가구의 월 수입 데이터를 '하위', '중위', '상위'로 분류하는 'Income_Level' 컬럼을 추가하고, 각 수입 수준에 해당하는 가구 수를 출력하세요. (구간: 0-200만원 미만: '하위', 200-400만원 미만: '중위', 400만원 이상: '상위')

income = pd.Series([150, 280, 420, 180, 350, 500, 220])

풀어보기

import pandas as pd

income = pd.Series([150, 280, 420, 180, 350, 500, 220])
bins = [0, 200, 400, float('inf')]
labels = ['하위', '중위', '상위']

income_level = pd.cut(income, bins=bins, labels=labels, right=False)
result = income_level.value_counts().sort_values(ascending=False)
result.name = "count"
print(result)

 

groupby와 pd.cut에 대해 알 수 있는 시간이었습니다. pd.cut보다 for문을 사용하는 방식을 더 편하게 생각했었는데, 사용해보니 pd.cut을 다들 사용하는 이유를 알겠더라구요..ㅎ.ㅎ
오늘 문제는 꽤 어려웠지만 이번 구문들 사용에 많은 도움이 되었던 시간이었습니다!!

 

오늘은 통계학 기초 4주차, 5주차에 대해 강의를 들었고, 남은 시간은 자소서 작성하는 데 시간을 보냈습니다!

자소서 작성은 정말 끝이 없는 것 같네요.. 벌써 10시가 넘어서 오늘은 이만 하고 내일을 준비해보도록 하겠습니다!

 

오늘도 수고하셨어요!