1+ #!/usr/bin/env python3
2+ """
3+ 20250327_가공식품DB_147999건.xlsx에서 제조사 정보를 추출하여
4+ food_clean_data.csv의 company_name을 업데이트하는 스크립트
5+ """
6+
7+ import pandas as pd
8+
9+ def main ():
10+ print ("제조사 정보 추출 및 매핑 시작..." )
11+
12+ try :
13+ # 1. 원본 Excel 파일 읽기
14+ print ("20250327_가공식품DB_147999건.xlsx 로딩 중..." )
15+ df_original = pd .read_excel ('/Users/kimminji/Desktop/Healthtant/20250327_가공식품DB_147999건.xlsx' )
16+ print (f"원본 데이터: { len (df_original )} 개 행" )
17+
18+ # 2. food_clean_data.csv 읽기
19+ print ("food_clean_data.csv 로딩 중..." )
20+ df_food = pd .read_csv ('/Users/kimminji/Desktop/Healthtant/food_clean_data.csv' )
21+ print (f"현재 식품 데이터: { len (df_food )} 개 행" )
22+
23+ # 3. 제조사 정보 추출 (식품코드 -> 제조사명)
24+ manufacturer_mapping = {}
25+ for idx , row in df_original .iterrows ():
26+ food_code = str (row ['식품코드' ]).strip ()
27+ manufacturer = str (row ['제조사명' ]).strip () if pd .notna (row ['제조사명' ]) else ''
28+
29+ # 제조사명이 없으면 수입업체명이나 유통업체명 사용
30+ if not manufacturer or manufacturer == 'nan' :
31+ manufacturer = str (row ['수입업체명' ]).strip () if pd .notna (row ['수입업체명' ]) else ''
32+ if not manufacturer or manufacturer == 'nan' :
33+ manufacturer = str (row ['유통업체명' ]).strip () if pd .notna (row ['유통업체명' ]) else ''
34+
35+ if manufacturer and manufacturer != 'nan' :
36+ manufacturer_mapping [food_code ] = manufacturer
37+
38+ print (f"제조사 정보를 찾은 식품: { len (manufacturer_mapping )} 개" )
39+
40+ # 4. food_clean_data.csv의 company_name 업데이트
41+ updated_count = 0
42+ for idx , row in df_food .iterrows ():
43+ food_id = str (row ['food_id' ]).strip ()
44+ if food_id in manufacturer_mapping :
45+ df_food .at [idx , 'company_name' ] = manufacturer_mapping [food_id ]
46+ updated_count += 1
47+
48+ print (f"업데이트된 식품: { updated_count } 개" )
49+
50+ # 5. 업데이트된 CSV 저장
51+ output_file = '/Users/kimminji/Desktop/Healthtant/food_clean_data_updated.csv'
52+ df_food .to_csv (output_file , index = False , encoding = 'utf-8' )
53+ print (f"업데이트된 파일 저장: { output_file } " )
54+
55+ # 6. 통계 출력
56+ total_foods = len (df_food )
57+ foods_with_manufacturer = len (df_food [df_food ['company_name' ].notna () & (df_food ['company_name' ] != '' ) & (df_food ['company_name' ] != 'UNKNOWN' )])
58+ print (f"\n === 통계 ===" )
59+ print (f"전체 식품: { total_foods } 개" )
60+ print (f"제조사 정보 있음: { foods_with_manufacturer } 개 ({ foods_with_manufacturer / total_foods * 100 :.1f} %)" )
61+
62+ # 7. 샘플 데이터 확인
63+ print (f"\n === 업데이트된 샘플 데이터 ===" )
64+ sample = df_food [df_food ['company_name' ].notna () & (df_food ['company_name' ] != '' ) & (df_food ['company_name' ] != 'UNKNOWN' )].head (10 )
65+ for _ , row in sample .iterrows ():
66+ print (f"ID: { row ['food_id' ]} , 식품명: { row ['food_name' ]} , 제조사: { row ['company_name' ]} " )
67+
68+ except Exception as e :
69+ print (f"오류 발생: { e } " )
70+ import traceback
71+ traceback .print_exc ()
72+
73+ if __name__ == "__main__" :
74+ main ()
0 commit comments