Data Analysis
Medical Insurance 비용 변화 분석 - Python(리스트,조건문 활용)
hyungminjeon
2025. 4. 10. 15:35
이번 실습에서는 조건문(if-else), 리스트 컴프리헨션, for문, while문 등을 활용하여 보험료 데이터의 평균을 구하고, 개별 보험료와 비교하는 로직을 작성했다.
1. 데이터 준비
먼저, 보험 가입자의 이름과 예상 보험료(estimated_insurance_costs) 및 실제 보험료(actual_insurance_costs) 데이터를 리스트로 저장했다.
names = ["Judith", "Abel", "Tyson", "Martha", "Beverley", "David", "Anabel"]
estimated_insurance_costs = [1000.0, 2000.0, 3000.0, 4000.0, 5000.0, 6000.0, 7000.0]
actual_insurance_costs = [1100.0, 2200.0, 3300.0, 4400.0, 5500.0, 6600.0, 7700.0]
각 가입자별 예상 보험료와 실제 보험료를 비교하여 분석할 것이다.
2. sum()과 len()을 활용한 평균 보험료 계산
Python의 내장 함수 sum()과 len()을 사용하여 평균 보험료를 구하는 간단한 방법을 적용했다.
print(sum(actual_insurance_costs)/len(actual_insurance_costs))
- sum(actual_insurance_costs): 실제 보험료의 총합을 계산한다.
- len(actual_insurance_costs): 보험료 리스트의 길이(개수)를 반환한다.
- 총합을 개수로 나누어 평균 보험료를 구한다.
출력 결과:
4400.0
3. for문을 사용하여 평균 보험료 계산
위에서 sum()을 활용한 간단한 방법을 사용했지만, 반복문을 활용하여 직접 평균을 계산하는 방식도 적용해 보았다.
total_cost = 0
for cost in actual_insurance_costs:
total_cost += cost
average_cost = total_cost / len(actual_insurance_costs)
print("Average Insurance Cost: " + str(average_cost) + " dollars")
- for cost in actual_insurance_costs: → 리스트의 각 요소를 순회
- total_cost += cost → 각 보험료 값을 total_cost에 누적
- 마지막에 total_cost / len(actual_insurance_costs) 연산을 수행하여 평균을 구함
출력 결과:
Average Insurance Cost: 4400.0 dollars
4. while문을 사용하여 평균 보험료 계산
이번에는 while문을 활용하여 동일한 연산을 수행했다.
total_cost = 0
index = 0
while index < len(actual_insurance_costs):
total_cost += actual_insurance_costs[index]
index += 1
average_cost = total_cost / len(actual_insurance_costs)
print("Average Insurance Cost: " + str(average_cost) + " dollars")
- index = 0 → 반복문을 위한 인덱스 초기화
- while index < len(actual_insurance_costs): → 리스트 끝까지 반복
- total_cost += actual_insurance_costs[index] → 각 요소를 total_cost에 누적
- index += 1 → 인덱스를 증가시켜 다음 요소로 이동
- 마지막에 평균을 계산하여 출력
출력 결과:
Average Insurance Cost: 4400.0 dollars
이전 for문을 사용한 방식과 동일한 결과를 얻을 수 있다.
5. 개별 보험료 분석
각 가입자의 실제 보험료를 출력하고, 평균 보험료와 비교하여 높거나 낮은 정도를 계산했다.
for i in range(len(names)):
name = names[i]
insurance_cost = actual_insurance_costs[i]
print("The insurance cost for " + name + " is " + str(insurance_cost) + " dollars")
if insurance_cost > average_cost:
print("The insurance cost for " + name + " is " + str(insurance_cost - average_cost) + " above average")
elif insurance_cost < average_cost:
print("The insurance cost for " + name + " is " + str(average_cost - insurance_cost) + " below average")
else:
print("The insurance cost for " + name + " is equal to the average")
- for i in range(len(names)): → 인덱스를 활용하여 names 리스트를 순회
- insurance_cost = actual_insurance_costs[i] → 현재 가입자의 보험료 가져오기
- if 조건문을 활용하여 보험료가 평균보다 높은지, 낮은지, 같은지를 판별
예를 들어, Judith의 보험료는 1100.0 dollars이며, 평균 보험료 4400.0 dollars보다 낮다.
출력 결과:
The insurance cost for Judith is 1100.0 dollars
The insurance cost for Judith is 3300.0 below average
...
The insurance cost for Anabel is 7700.0 dollars
The insurance cost for Anabel is 3300.0 above average
6. 리스트 컴프리헨션을 활용한 예상 보험료 업데이트
estimated_insurance_costs 리스트의 모든 값을 10% 증가시켜 업데이트했다.
updated_estimated_costs = [cost * 11/10 for cost in estimated_insurance_costs]
print(updated_estimated_costs)
- [연산 for 변수 in 리스트] → 리스트 컴프리헨션 사용
- cost * 11/10 → 각 보험료 값을 10% 증가
출력 결과:
[1100.0, 2200.0, 3300.0, 4400.0, 5500.0, 6600.0, 7700.0]
이제 예상 보험료가 기존보다 10% 증가한 새로운 리스트로 변환되었다.
7. 정리
이번 실습에서는 Python의 조건문, for문, while문, 리스트 컴프리헨션을 활용하여 보험료 데이터를 분석하는 방법을 배웠다.
✅ 핵심 내용 정리
개념설명
sum()과 len() 활용 | 리스트의 합과 개수를 이용하여 평균값 계산 |
for문 사용 | 리스트의 각 요소를 순회하며 합산하여 평균값 계산 |
while문 사용 | 인덱스를 이용하여 리스트 요소를 순회하며 평균값 계산 |
if-elif-else 조건문 | 개별 보험료가 평균보다 높은지, 낮은지 판단 |
리스트 컴프리헨션 | 예상 보험료를 일괄적으로 10% 증가 |