Spaces:
Running
Running
File size: 1,446 Bytes
21bdc64 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # λ€μ΄λ² μμ ν€μλ dfλ₯Ό λμ보λ Overviewμ© μ§κ³ κ²°κ³Όλ‘ λ³ννλ λͺ¨λ
import pandas as pd
def build_overview(keyword: str, df: pd.DataFrame) -> dict:
# μμ df β μΉ΄λ/ν
μ΄λΈμ© μ§κ³ dict λ°ν
if df.empty:
return {
"keyword": keyword,
"keyword_count": 0,
"total_search_volume": 0,
"competition_breakdown": {},
"top_keywords": [],
"note": "μ°κ΄ ν€μλκ° μμ΅λλ€. ν€μλλ₯Ό νμΈνμΈμ.",
}
df = df.copy()
df["total_volume"] = df["search_volume_pc"].fillna(0) + df["search_volume_mobile"].fillna(0)
return {
"keyword": keyword,
"keyword_count": int(len(df)),
# μ΄ κ²μλμ ν©κ³. λ¨ λμ¨νκ² μ°κ΄λ λ²μ© ν€μλκ° μμ¬ μμ΄,
# μ λ° μ§κ³λ 2λ¨κ³ ν΄λ¬μ€ν°λ§ λ
Έμ΄μ¦ νν°λ§ νλ‘ λ―Έλ£¬λ€.
"total_search_volume": int(df["total_volume"].sum()),
"median_volume_per_keyword": int(df["total_volume"].median()),
"competition_breakdown": df["competition_idx"].value_counts().to_dict(),
"masked_count": int(df["is_masked"].sum()),
"top_keywords": (
df.nlargest(10, "total_volume")[
["keyword", "search_volume_pc", "search_volume_mobile",
"total_volume", "competition_idx"]
].to_dict(orient="records")
),
}
|