cabbage_dataset / CabbageRetail.py
hyeon2525's picture
Upload folder using huggingface_hub
4a11767 verified
import pandas as pd
import os
# CabbagePrice_merged.xlsx 파일 읽기
retail_path = 'CabbagePrice_merged.xlsx'
retail_df = pd.read_excel(retail_path)
# '평균가격' 컬럼이 있는지 확인하고, 없으면 '당일' 컬럼 사용
if '평균가격' in retail_df.columns:
price_col = '평균가격'
elif '당일' in retail_df.columns:
price_col = '당일'
else:
raise ValueError("가격 정보를 찾을 수 없습니다.")
# ',' 제거하고 평균가격 숫자로 변환
retail_df['avg_price'] = (
retail_df[price_col]
.astype(str)
.str.replace(',', '')
.astype(float) # 먼저 float으로 변환
.round() # 반올림
.astype(int) # 최종 정수형 변환
)
# 평균가격이 0이 아닌 행만 필터링
retail_df = retail_df[retail_df['avg_price'] != 0].copy()
# 날짜 분해
retail_df[['year', 'month', 'day']] = retail_df['DATE'].astype(str).str.split('-', expand=True).astype(int)
# 가격 차이(gap) 계산
retail_df.sort_values('DATE', inplace=True)
retail_df['gap'] = retail_df['avg_price'].diff().fillna(0).astype(int)
# 최종 열 선택 및 정렬
final_retail_df = retail_df[['year', 'month', 'day', 'avg_price', 'gap']]
final_retail_df = final_retail_df.sort_values(['year', 'month', 'day'])
# 저장 (store 폴더 생성)
os.makedirs('store', exist_ok=True)
output_path = 'store/cabbage_retail.csv'
final_retail_df.to_csv(output_path, index=False)
print(f"총 {len(final_retail_df)}행 처리 완료")
print(f"날짜 범위: {final_retail_df['year'].min()}-{final_retail_df['month'].min()}-{final_retail_df['day'].min()} ~ {final_retail_df['year'].max()}-{final_retail_df['month'].max()}-{final_retail_df['day'].max()}")
print(f"\n처음 5행:")
print(final_retail_df.head())
print(f"\n저장 완료: {output_path}")