Spaces:
Sleeping
Sleeping
SimHS98 commited on
Commit ·
8eb2cfe
1
Parent(s): 7f535fb
267 unmodified lines268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310267 unmodified lines# 2️⃣ Sales / Ads 자동 분리 함수def transform_to_standard_format(df):if df.empty:return pd.DataFrame(), pd.DataFrame()df["Date"] = pd.to_datetime(df["Date"], errors="coerce")# 국가/채널 추정def detect_country(name):for c in ["SG", "MY", "PH", "TW", "TH", "VN", "US"]:if c in name: return creturn "Unknown"def detect_channel(name):name = name.lower()if "shopify" in name: return "Shopify"if "shopee" in name: return "Shopee"if "amazon" in name or "asin" in name: return "Amazon"if "tiktok" in name: return "TikTok"return "Unknown"# 매출/광고 컬럼 분리sales_cols = [c for c in df.columns if any(k in c for k in ["매출", "sales", "Revenue", "GMV"])]ads_cols = [c for c in df.columns if any(k in c for k in ["지출", "광고", "ad", "spend", "cost"])]# --- stg_Sales ---df_sales = pd.melt(df, id_vars=["Date"], value_vars=sales_cols, var_name="Metric_Raw", value_name="Amount_Local")df_sales["Country"] = df_sales["Metric_Raw"].apply(lambda x: detect_country(x))df_sales["Channel"] = df_sales["Metric_Raw"].apply(lambda x: detect_channel(x))df_sales["Currency"] = df_sales["Metric_Raw"].apply(lambda x: "KRW" if "KRW" in x else "Local")df_sales["Amount_KRW"] = df_sales["Amount_Local"]df_sales = df_sales[["Date", "Country", "Channel", "Amount_Local", "Currency", "Amount_KRW"]]# --- stg_Ads ---df_ads = pd.melt(df, id_vars=["Date"], value_vars=ads_cols, var_name="Metric_Raw", value_name="Amount_Local")df_ads["Country"] = df_ads["Metric_Raw"].apply(lambda x: detect_country(x))df_ads["Channel"] = df_ads["Metric_Raw"].apply(lambda x: detect_channel(x))df_ads["Currency"] = df_ads["Metric_Raw"].apply(lambda x: "KRW" if "KRW" in x else "Local")df_ads["Amount_KRW"] = df_ads["Amount_Local"]df_ads["Campaign"] = ""df_ads = df_ads[["Date", "Country", "Channel", "Campaign", "Amount_Local", "Currency", "Amount_KRW"]]267 unmodified lines268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332267 unmodified lines# 2️⃣ Sales / Ads 자동 분리 함수def transform_to_standard_format(df):"""3단계 멀티헤더 기반 단순 분류 버전- 1행: Country (SG, MY, ...)- 2행: Type (매출, 지출, ROAS, 환율)- 3행: Channel (쇼피파이, Meta, 내부 광고 등)"""if df.empty:return pd.DataFrame(), pd.DataFrame()# 1️⃣ 날짜 컬럼 지정if "Date" not in df.columns:df.rename(columns={df.columns[0]: "Date"}, inplace=True)df["Date"] = pd.to_datetime(df["Date"], errors="coerce")# 2️⃣ 컬럼명 분해melt_cols = [c for c in df.columns if c != "Date"]records = []for col in melt_cols:parts = col.split("_")if len(parts) == 3:country, dtype, channel = partselif len(parts) == 2:country, dtype = partschannel = "Total"else:continue # 혹시 예외적 컬럼이 있으면 무시# 3️⃣ Sales(매출) / Ads(지출) / 기타 구분if dtype not in ["매출", "지출"]:continuefor idx, val in enumerate(df[col]):if val not in [None, "", " ", "-", 0]:records.append({"Date": df.loc[idx, "Date"],"Country": country.strip(),"Type": dtype.strip(),"Channel": channel.strip(),"Amount_Local": val})df_flat = pd.DataFrame(records)# 4️⃣ 통화 추정df_flat["Currency"] = df_flat["Channel"].apply(lambda x: "KRW" if "KRW" in x or "₩" in x else("USD" if "USD" in x else("SGD" if any(k in x for k in ["SGD", "S$"]) else "Local")))df_flat["Amount_KRW"] = df_flat["Amount_Local"]# 5️⃣ Sales / Ads 구분 (2행 dtype 기준)df_sales = df_flat[df_flat["Type"] == "매출"].copy()df_ads = df_flat[df_flat["Type"] == "지출"].copy()# 6️⃣ 컬럼 순서 정리df_sales = df_sales[["Date", "Country", "Channel", "Amount_Local", "Currency", "Amount_KRW"]]df_ads["Campaign"] = ""df_ads = df_ads[["Date", "Country", "Channel", "Campaign", "Amount_Local", "Currency", "Amount_KRW"]]
main.py+53-31