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")
        ),
    }