아이티윌_데이터 분석 55기/문제풀이_Python

#8-2. 8일차 퀴즈에 대한 문제풀이

ecosso 2026. 5. 20. 17:06

[작업형 1유형]
2. airquality.csv 데이터는 Ozone, Solar.R, Wind, Temp, Month, Day로 이루어져 있다. 
Ozone, Solar.R, Temp에 대해 각 변수의 결측값의 개수를 구하고, 
결측값의 개수가 가장 많은 변수에 대해 결측값을 해당 변수값들의 중앙값으로 대체하시오. 
또한, 결측값의 개수가 가장 많은 변수에 대해 결측값 대체 전과 대체 후
의 평균값을 각각 출력하시오. 
(단, 반올림하여 소수점 둘째 자리까지 출력하시오.)

df.head()
Out[678]: 
   Unnamed: 0  Ozone  Solar.R  Wind  Temp  Month  Day
0           1   41.0    190.0   7.4    67      5    1
1           2   36.0    118.0   8.0    72      5    2
2           3   12.0    149.0  12.6    74      5    3
3           4   18.0    313.0  11.5    62      5    4
4           5    NaN      NaN  14.3    56      5    5

 

더보기

[ 내 답변 ]

# 데이터 불러오기
df = pd.read_csv('airquality.csv')
df.head()

# 결측치 확인하기
pd.isnull(df).sum()

pd.isnull(df).sum()
Out[685]: 
Unnamed: 0     0
Ozone         37
Solar.R        7
Wind           0
Temp           0
Month          0
Day            0
dtype: int64

 

ozone_na = 37
solarr_na = 7
temp = 0

print(ozone_na)
print(solarr_na)
print(temp)

print(ozone_na)
37

print(solarr_na)
7

print(temp)
0

 

# 결측값의 개수가 가장 많은 변수에 대해 결측값을 해당 변수값들의 중앙값으로 대체
df.head()
mid = df['Ozone'].median()
df['Ozone2'] = df['Ozone'].fillna(mid)

## 대체 전
alt1 = round(df['Ozone'].mean(), 2)
print(alt1)

print(alt1)
42.13


## 대체 후
alt2 = round(df['Ozone2'].mean(), 2)
print(alt2)

print(alt2)
39.56

[ 문제풀이 ]

 

# 결측값의 수
vars = df1.isnull().sum().idxmax()
a1 = df1[vars].mean()
a2 = df1[vars].fillna(df1[vars].median()).mean()

print(round(a1,2))
print(round(a2,2))

print(round(a1,2))
42.13

print(round(a2,2))
39.56

 


3. order.csv 데이터는 cus_id(고객번호), order_no(주문번호), purchase(구매액), 
cancel(구매취소액), rebuy(재구매여부)로 이루어져 있다. 다음 결과를 출력하시오. 
(1) 구매액과 구매취소액의 차이를 구한 후, 차이 금액의 절댓값이 가장 큰 order_no를 모두 출력하시오. 
(2) 재구매(rebuy=T)한 고객들 중에서 구매액과 구매취소액의 절댓값이 가장 작은 고객에 대한 
구매액의 합계를 출력하시오.

df1.head()
Out[730]: 
  cus_id order_no  purchase  cancel rebuy
0  C5428  C190117     276.8    54.0     T
1  C5428  C190120     244.4    39.6     T
2  C5428  C190122     266.0    51.6     T
3  C5428  C190127     262.4    52.8     T
4  C5428  C190133     263.6    51.6     T

 

더보기

[ 내 답변 ]

# 데이터 불러오기
df1 = pd.read_csv('order.csv')
df1.head()

# (1) 구매액과 구매취소액의 차이를 구한 후, 차이 금액의 절댓값이 가장 큰 order_no를 모두 출력하시오. 
# 구매액 - 구매취소액
df1['diff'] = abs(df1['purchase'] - df1['cancel'])
df2 = df1.loc[df1['diff'] == df1['diff'].max(), :]
print(df2['order_no'])

print(df2['order_no'])
11    C190146
53    C190190
Name: order_no, dtype: object

 

# (2) 재구매(rebuy=T)한 고객들 중에서 구매액과 구매취소액의 절댓값이 가장 작은 고객에 대한 구매액의 합계를 출력하시오.
df3 = df1.loc[df1['rebuy'] == 'T', :]
df4 = df3.groupby('cus_id')['diff'].sum()
df4.sort_values()

df4.sort_values()
Out[749]: 
cus_id
C5427    2047.2
C5426    3568.4
C5428    6246.0
Name: diff, dtype: float64


print('2047.2')


 

[ 문제풀이 ]

# (1)
df2['diff'] = (df2['purchase'] - df2['cancel']).abs()
result1 = df2.loc[df2['diff'] == df2['diff'].max(), 'order_no'].values
print(result1)

print(result1)
['C190146' 'C190190']

 

# (2)
df3 = df2.loc[df2['rebuy'] == 'T', : ]
cid = df3.groupby('cus_id')['diff'].sum().idxmin()
result3 = df2.loc[df2['cus_id'] == cid, 'purchase'].sum()
print(result3)

print(result3)
6408.0