Today I learned
1. QAQC 문제풀이 가이드
1) 결측치와 특정 값 동시에 처리하기
import pandas as pd
import numpy as np
df = pd.DataFrame({
'ID': [1, 2, 3, 4],
'Score': [75, np.nan, 88, 92],
'Status': ['Completed', 'Unknown', 'Completed', 'Unknown']})
df['Status'] = df['Status'].replace('Unknown','Pending')
#Status 컬럼의 Unknown을 Pending으로 교체
df['Score'] = df['Score'].replace(np.nan, 0)
#np.nan 데이터를 0으로 교체
2) 라인별 불량 유형 비중 Pie
import pandas as pd
import matplotlib.pyplot as plt
# 라인별 불량 유형 건수
data = {
'line': ['A','A','A','B','B','C','C','C'],
'defect_type':['Scratch','Particle','Crack','Scratch','Crack','Particle','Crack','Scratch'],
'count': [30, 20, 10, 25, 15, 18, 22, 20]
}
df = pd.DataFrame(data)
df_A = df[df['line'] == 'A']
#line 칼럼에서 A만 필터링
summary = df_A.groupby('defect_type')['count'].sum()
#defect_type 데이터에서 각 컬럼에 해당하는 count 값을 더하여 보여줌
top3 = summary.sort_values(ascending=False).head(3) # 상위 3개
others = summary.sort_values(ascending=False).iloc[3:].sum() # 나머지 합산
#내림차순으로 정렬 후 상위 3개의 값을 보여줌
#iloc[3:]로 3열 아래 값들을 모두 더하여 보여줌
# 4) 파이차트 시각화
plt.pie(top3, labels=top3.index, autopct='%1.1f%%', startangle=90)
plt.title("Line A - Defect Type Distribution")
plt.show()
3)일자별 불량률(line)
import pandas as pd
import matplotlib.pyplot as plt
# 일자별 공정 실적(생산/불량)
data = {
'date': ['2025-09-28','2025-09-28','2025-09-29','2025-09-29','2025-09-30','2025-09-30'],
'line': ['A','B','A','B','A','B'],
'production': [500, 450, 520, 470, 510, 490],
'defects': [15, 10, 18, 12, 16, 11]
}
df = pd.DataFrame(data)
#1)
hapgyae = df.groupby('date')[['production','defects']].sum()
#2)
hapgyae['defect_rate'] = hapgyae['defects'] / hapgyae['production'] * 100
#3)
plt.plot(hapgyae.index, hapgyae['defect_rate'], marker = 'o')
#4)
plt.xlabel("date")
plt.ylabel("defect rate %")
plt.show()
#groupby로 date에 해당하는 production과 defect값을 더하여 hapgyae 변수에 저장
#hapgyae 변수의 defect rate 컬럼을 만들고, 해당 컬럼에는 defect와 production과의 연산을 저장
4) 나이대별 그룹화
import pandas as pd
ages = pd.Series([15, 22, 31, 45, 18, 28, 37, 42])
bins = [10, 20, 30, 40, 50]
labels = ['10대', '20대','30대','40대 이상']
df = pd.DataFrame({'Age':ages})
df['Age_group'] = pd.cut(df['Age'], bins = bins, labels = labels, right = False)
df1=df['Age_group'].value_counts().sort_index()
print(df1)
#pd.cut을 이용하여 시리즈에서 조건에 맞는 값을 찾음
'빅데이터 QAQC_3기 > 빅데이터 QAQC_3기 TIL' 카테고리의 다른 글
| TIL_251014 (0) | 2025.10.14 |
|---|---|
| TIL_251013 (0) | 2025.10.13 |
| TIL_251001 (0) | 2025.10.01 |
| TIL_250930 (0) | 2025.09.30 |
| TIL_250929 (0) | 2025.09.29 |