เค•ोเคˆ เคชเคฐिเคฃाเคฎ เคจเคนीं เคฎिเคฒा

    ๐Ÿ“˜ Day 8: Pandas Data Cleaning (Missing Data & Duplicates) - python in ml and data scientist

    Day 8 — Pandas Data Cleaning (Missing data & Duplicates)

    เคฏเคน เคชूเคฐा static, Blogger-friendly chapter เคนै — เคนเคฐ section เคฎें theory + explaination + Python (pandas) code snippets + expected output tables เคฆिเค เค—เค เคนैं, เคคाเค•ि เค†เคช เค‡เคธเค•ो เคธीเคงे เค…เคชเคจे เคฌ्เคฒॉเค— เคฎें paste เค•เคฐ เค•े เคชเคข़ा/เคช्เคฐเคถिเค•्เคทिเคค เค•เคฐ เคธเค•ें।

    เคจोเคŸ: เคฏเคน Static HTML เคนै — เคฎैंเคจे interactive (Pyodide) script เคนเคŸा เคฆी เคนै เคคाเค•ि Blogger เคœैเคธे เคช्เคฒेเคŸเคซ़ॉเคฐ्เคฎ เคชเคฐ เคฌिเคจा external scripts เค•े เคญी เคฏเคน เคธเคนी เคธे เคฆिเค–े।

    เคธैเคฆ्เคงाเคจ्เคคिเค• เคชเคฐिเคšเคฏ — Pandas Data Cleaning เค•्เคฏा เคนै?

    เคชाเค‡เคฅเคจ เคฎें pandas เคเค• เคถเค•्เคคिเคถाเคฒी เคฒाเค‡เคฌ्เคฐेเคฐी เคนै เคœिเคธเค•ा เคช्เคฐเคฏोเค— table-like data (DataFrame) เค•े เคธाเคฅ เค•िเคฏा เคœाเคคा เคนै।Data Cleaning เค•ा เคฎเคคเคฒเคฌ เคนै: raw/real-world datasets เคฎें เค†เคจे เคตाเคฒी เค—เคฒเคคिเคฏों เค”เคฐ inconsistencies เค•ो เค ीเค• เค•เคฐเคจा เคคाเค•ि analysis, visualization เค”เคฐ machine learning models เคชเคฐ เคญเคฐोเคธा เค•िเคฏा เคœा เคธเค•े।

    • Missing values (NaN, None, blanks): detect เค•เคฐเคจे เค•े เคฒिเค isnull(), เคนเคŸाเคจे เค•े เคฒिเค dropna(), เคญเคฐเคจे เค•े เคฒिเค fillna().
    • Duplicates: duplicate rows เคฏा duplicate identifiers — detect เค•เคฐเคจे เค•े เคฒिเค duplicated(), เคนเคŸाเคจे เค•े เคฒिเค drop_duplicates().
    • Consistency: data types เคธเคนी เค•เคฐเคจा (astype()), strings clean เค•เคฐเคจा (str.strip(), lower/upper), เค”เคฐ invalid values เค•ो replace เค•เคฐเคจा।

    เคเค• เคธ्เคชเคท्เคŸ เค‰เคฆाเคนเคฐเคฃ: เคฏเคฆि sales data เคฎें เค•ुเค› rows เค•ी price missing เคนै, เคคो total revenue เค—เคฒเคค เคจिเค•เคฒेเค—ा; เคฏा เคฏเคฆि เคเค• customer เค•ी เคฐिเค•ॉเคฐ्เคก duplicate เคนै เคคो analysis เคฎें overcount เคนोเค—ा।

    เค•्เคฏों เคธीเค–เคจा เคšाเคนिเค? (Importance)

    1. Real-world data เค•เคญी เคญी perfect เคจเคนीं เคนोเคคा — cleaning เคœเคฐूเคฐी เคนै।
    2. Dirty data เคธे เค—เคฒเคค insights เค”เคฐ เค—เคฒเคค business decisions เคนो เคธเค•เคคे เคนैं।
    3. Machine learning models clean data เคชเคฐ เคนी เคธเคนी เคธीเค–เคคे เคนैं — missing/duplicate data accuracy เค˜เคŸाเคคे เคนैं।
    4. เคช्เคฐैเค•्เคŸिเค•เคฒ data scientist เคฌเคจเคจे เค•े เคฒिเค เคฏเคน เคธเคฌเคธे เคชเคนเคฒी skill เคนै।
    5. Data cleaning เคธे reporting, visualization เค”เคฐ storage เคฌेเคนเคคเคฐ เคนोเคคे เคนैं।

    เค•เคนाँ เค”เคฐ เค•ैเคธे เค‡เคธ्เคคेเคฎाเคฒ เค•เคฐेंเค—े?

    Data cleaning เคนเคฐ เคœเค—เคน เค•ाเคฎ เค†เคคा เคนै — e-commerce, banking, healthcare, survey data, education เค†เคฆि। เคธाเคฎाเคจ्เคฏ workflow:

    1. Data load: pd.read_csv(), pd.read_excel()
    2. Inspect: shape, head(), info(), describe(), missing counts (isnull().sum())
    3. Handle missing: drop เคฏा fill (mean/median/0/forward-fill/ custom)
    4. Handle duplicates: identify using duplicated() เค”เคฐ drop_duplicates()
    5. Data type conversions, trimming strings, replace invalid values
    6. Final check เค”เคฐ save

    เค…เคฌ เค†เคชเค•े เคนเคฐ เคช्เคฐเคถ्เคจ เค•े เคตिเคธ्เคคृเคค เคœเคตाเคฌ — เคนเคฐ เคเค• เค•े 5 เค‰เคฆाเคนเคฐเคฃ (Step-by-step + code + expected output)

    Q1: "Pandas Data Cleaning เค•्เคฏा เคนै?" — 5 Examples

    เคจीเคšे เคนเคฐ example เค›ोเคŸे dataframes เค•े เคธाเคฅ เคฆिเค–ाเคฏा เค—เคฏा เคนै — code เคชเคข़िเค เค”เคฐ เค‰เคธเค•े เคจीเคšे expected output table เคฎौเคœूเคฆ เคนै เคคाเค•ि beginner เคญी เคธเคฎเค เคธเค•े เค•ि เค•्เคฏा เคนोเคคा เคนै।

    Example 1 — Missing values detection (identify เค•เคฐเคจा)

    import pandas as pd
    
    df = pd.DataFrame({ 'Name': ['Amit', 'Ravi', 'Sita', None], 'Age': [25, None, 30, 28] })
    
    missing counts
    
    print(df) print(' Missing counts:') print(df.isnull().sum())

    Expected output (table):

    NameAge
    Amit25.0
    Ravi
    Sita30.0
    28.0

    Missing counts: Name: 1, Age: 1 — เคฏเคน เคฌเคคाเคคा เคนै เค•िเคจ columns เคฎें NaN เคนै।

    Example 2 — Fill missing with mean

    # same df as above
    
    fill missing age with mean age
    
    mean_age = df['Age'].mean() df['Age'] = df['Age'].fillna(mean_age) print(df)

    Expected: Age column เค•े missing เค•ो average เคธे replace เค•เคฐ เคฆेंเค—े; Name เค•ा missing เค…เคญी เคญी เคฐเคนेเค—ा เค…เค—เคฐ เคนเคฎ เค‰เคธे drop/replace เคจเคนीं เค•เคฐเคคे।

    Example 3 — Drop missing rows

    # drop any row with at least one NaN
    
    clean = df.dropna() print(clean)

    Expected: เคตो เคธाเคฐी rows เคนเคŸेंเค—ी เคœिเคจเคฎें เค•ोเคˆ NaN เคนै — เค•ेเคตเคฒ fully complete rows เคฌเคšेंเค—ी।

    Example 4 — Detect duplicates

    df2 = pd.DataFrame({'id':[1,1,2,3],'val':[10,10,20,30]})
    
    print(df2) print('duplicated mask:') print(df2.duplicated())

    Expected: duplicated() boolean series เคฆेเค—ा — True เคตเคนां เคœเคนां same row เคชเคนเคฒे เค† เคšुเค•ा เคนै।

    Example 5 — Remove duplicates

    df2_unique = df2.drop_duplicates()
    
    print(df2_unique)

    Expected: duplicate row remove เคนो เคœाเคเค—ी เค”เคฐ unique rows เคฌเคšेंเค—ी।


    Q2: Pandas Data Cleaning เค•ा Use เค•्เคฏा เคนै? — 5 Examples

    Use-cases เค›ोเคŸे real-world contexts เคฎें — เคนเคฐ เคเค• เค•े เคธाเคฅ code + expected result:

    Example 1 — Sales dataset: missing price -> fill with mean price

    df_sales = pd.DataFrame({'product':['A','B','C','D'],'price':[100, None, 150, None]})
    
    fill missing
    
    df_sales['price'] = df_sales['price'].fillna(df_sales['price'].mean()) print(df_sales)

    Expected: B เค”เคฐ D เค•ी price average เคธे replace เคนो เคœाเคเค—ी — เคœिเคธเคธे total revenue calculate เค•เคฐเคจा meaningful เคนोเค—ा।

    Example 2 — Hospital: duplicate patient records เคนเคŸाเคจा

    df_pat = pd.DataFrame({'patient_id':[101,102,101,103],'name':['R','S','R','T'],'age':[30,25,30,40]})
    
    remove duplicates based on patient_id
    
    df_pat_unique = df_pat.drop_duplicates(subset=['patient_id']) print(df_pat_unique)

    Expected: patient_id 101 duplicate entry remove เคนोเค—ी — accurate patient count เคฎिเคฒेเค—ा।

    Example 3 — School: missing marks -> fill with 0 (absent)

    df_marks = pd.DataFrame({'student':['A','B','C'],'marks':[80, None, 90]})
    
    df_marks['marks'] = df_marks['marks'].fillna(0) print(df_marks)

    Expected: B เค•ा marks 0 set เคนोเค—ा, เคœिเคธเคธे class average calculate เค•เคฐเคจा straightforward เคนोเค—ा।

    Example 4 — Banking: missing transaction amounts -> drop those rows

    df_tx = pd.DataFrame({'tx_id':[1,2,3,4],'amount':[200,None,500,None]})
    
    df_tx_clean = df_tx.dropna(subset=['amount']) print(df_tx_clean)

    Expected: เค•ेเคตเคฒ valid transactions เคฐเคนेंเค—ी; null amounts ignore เคนो เคœाเคँเค—ी।

    Example 5 — E-commerce: duplicate orders remove by order_id

    df_orders = pd.DataFrame({'order_id':[1001,1002,1001,1003],'customer':['X','Y','X','Z'],'value':[500,300,500,400]})
    
    df_orders = df_orders.drop_duplicates(subset=['order_id']) print(df_orders)

    Expected: duplicate order 1001 remove — correct order count เค”เคฐ total revenue เคฎिเคฒेเค—ी।


    Q3: เค‡เคธे เค•्เคฏों เคธीเค–เคจा เคšाเคนिเค? (5 Examples — consequences of NOT cleaning)

    เคฏเคนाँ เคนเคฎ เคฆिเค–ाเคँเค—े เค•ी เค…เค—เคฐ data clean เคจ เค•िเคฏा เคœाเค เคคो เค•्เคฏा เค—เคฒเคค เคนोเค—ा (simple, clear examples)।

    Example 1 — Missing values change statistics (mean wrong)

    df = pd.DataFrame({'val':[10, None, 30]})
    
    print('mean (skips NaN):', df['val'].mean())
    
    เค…เค—เคฐ เคนเคฎ NaN เค•ो 0 เคฎाเคจเค•เคฐ add เค•เคฐें เคคो result เคฌเคฆเคฒेเค—ा
    
    print('sum skipping NaN:', df['val'].sum())

    Explanation: Statistics calculation default behavior NaN เค•े เคธाเคฅ เค…เคฒเค— เคนोเคคी เคนै — เค…เค—เคฐ logically NaN เค•ो 0 เคฎाเคจเคจा เคนै เคคो fillna(0) เค•เคฐें, เคตเคฐเคจा leave เค•เคฐें।

    Example 2 — Duplicates cause overcount

    dfdup = pd.DataFrame({'item':['A','A','B'],'qty':[1,1,2]})
    
    print('rows before:', len(dfdup)) print(dfdup) dfdup2 = dfdup.drop_duplicates() print('rows after:', len(dfdup2))

    Explanation: Duplicate rows เคนोเคจे เคธे aggregates (เคœैเคธे total items) เค—เคฒเคค เคนोंเค—े।

    Example 3 — Arithmetic with NaN propagates NaN

    dfA = pd.DataFrame({'a':[1, None, 3]})
    
    print((dfA['a'] + 2).tolist())

    Explanation: NaN arithmetic เคฎें เคฐเคนे เคคो final results เคฎें NaN เค† เคธเค•เคคे เคนैं — preprocessing เคฎें fill เคฏा drop เค•เคฐเคจा เคšाเคนिเค।

    Example 4 — Healthcare: duplicate patient distorts counts

    df_pat = pd.DataFrame({'pid':[1,2,1,3]})
    
    print('unique patients:', df_pat['pid'].nunique()) print('rows before:', len(df_pat)) print(df_pat.drop_duplicates())

    Explanation: Duplicate patient rows เคธे patient count เค”เคฐ per-patient stats เค—เคฒเคค เคนोंเค—े।

    Example 5 — Survey completeness percent

    df_s = pd.DataFrame({'q1':[1,None,0], 'q2':[None,1,1]})
    
    print('overall completeness (%) =', df_s.notnull().mean().mean()*100)

    Explanation: เค…เค—เคฐ completeness เค•เคฎ เคนै เคคो survey results reliable เคจเคนीं เคนोंเค—े — cleaning เค”เคฐ imputation เค•ैเคธे เค•เคฐेंเค—े เคฏเคน design decision เคนै।


    Q4: เค‡เคธे เค•เคนाँ เค”เคฐ เค•ैเคธे use เค•เคฐेंเค—े? (5 Practical examples)

    Small, real preprocessing patterns เคœो เค†เคช data pipelines เคฎें เคฌाเคฐ-เคฌाเคฐ เคฆेเค–ेंเค—े:

    Example 1 — E-commerce: dedupe customer table for recommender

    df = pd.DataFrame({'cust_id':[1,2,1,3],'visits':[5,2,5,1]})
    
    dedupe keeping first
    
    df_u = df.drop_duplicates(subset=['cust_id']) print(df_u)

    Use: Recommendation models per-user stats เคฌเคจाเคคे เคธเคฎเคฏ duplicate rows problem เคนोเค—ा — dedupe เค•เคฐเค•े fix เค•เคฐें।

    Example 2 — Banking: remove null transaction amounts before fraud model

    df = pd.DataFrame({'tx':[1,2,3],'amt':[100,None,200]})
    
    df_clean = df.dropna(subset=['amt']) print(df_clean)

    Use: ML model เค•ो numeric amount เคšाเคนिเค — null rows เคฎॉเคกเคฒ เคฎें confuse เค•เคฐेंเค—ी।

    Example 3 — Healthcare: impute missing age with median

    df = pd.DataFrame({'pid':[1,2,3],'age':[25,None,45]})
    
    df['age'] = df['age'].fillna(df['age'].median()) print(df)

    Use: Median imputation outliers เคธे เค•เคฎ เคช्เคฐเคญाเคตिเคค เคนोเคคा เคนै।

    Example 4 — Education: forward-fill attendance for continuous days

    df = pd.DataFrame({'day':[1,2,3,4],'present':[1,None,None,1]})
    
    df['present'] = df['present'].fillna(method='ffill') print(df)

    Use: Continuous time-series เคฎें forward/backward fill practical เคนोเคคा เคนै।

    Example 5 — Govt survey: drop respondents with too many blanks

    df = pd.DataFrame({'r':[1,2,3],'a':[1,None,None],'b':[None,2,None],'c':[3,4,None]})
    
    keep rows with fewer than 2 blanks
    
    df_clean = df[df.isnull().sum(axis=1) < 2] print(df_clean)

    Use: Low-quality respondents remove เค•เคฐเค•े overall dataset quality improve เค•เคฐें।


    Q5: เค‡เคธे เค•्เคฏों use เค•เคฐเคจा เคšाเคนिเค? (5 Examples — benefits)

    Benefits เค•ो เค›ोเคŸे เค‰เคฆाเคนเคฐเคฃों เคฎें เคฆिเค–ा เคฐเคนे เคนैं:

    Example 1 — Stable statistics after cleaning

    df = pd.DataFrame({'v':[10,None,30,40]})
    
    df2 = df.copy() df2['v'] = df2['v'].fillna(df2['v'].mean()) print('mean before:', df['v'].mean()) print('mean after:', df2['v'].mean()) print(df2)

    Benefit: Imputation เคธे statistics consistent เคนोंเค—े — reporting reliable เคฌเคจेเค—ा।

    Example 2 — Remove duplicates saves rows and storage

    df = pd.DataFrame({'id':[1,1,2,3]})
    
    print('before rows:', len(df)) df_u = df.drop_duplicates() print('after rows:', len(df_u))

    Benefit: Deduplication เคธे storage เค—िเคฐेเค—ा เค”เคฐ analysis เคธเคนी เคนोเค—ा।

    Example 3 — Arithmetic works after fill

    df = pd.DataFrame({'a':[1,None,3]})
    
    res = (df['a'].fillna(0) + 2).tolist() print(res)

    Benefit: Calculations predictable เคนोंเค—ी; NaN เคธे unexpected errors เคจเคนीं เค†เคँเค—े।

    Example 4 — Visualization ready (no gaps)

    df = pd.DataFrame({'x':[1,2,3],'y':[10,None,30]})
    
    df['y'] = df['y'].fillna(method='ffill') print(df)

    Benefit: Visual plots เคฎें gaps เคจเคนीं เคฆिเค–ेंเค—े — trends เคธाเคซ เคฆिเค–ेंเค—े।

    Example 5 — Business totals correct

    df = pd.DataFrame({'sale':[100,None,200]})
    
    print('sum default (NaN ignored):', df['sale'].sum()) df['sale'] = df['sale'].fillna(0) print('sum after fillna(0):', df['sale'].sum())

    Benefit: Total sales calc เค•เคฐเคจे เค•े เคฒिเค NaN เค•ो 0 เคฎाเคจเคจा เคœเคฐूเคฐी เคนो เคธเค•เคคा เคนै — cleaning เคธे business metrics เคธเคนी เคฎिเคฒेंเค—े।


    Provided for learners of beepship.blogspot.com — Day 8 Pandas Data Cleaning chapter python in machine learning and data scientist.

    เคเค• เคŸिเคช्เคชเคฃी เคญेเคœें

    เค”เคฐ เคจเคฏा เคชुเคฐाเคจे

    ู†ู…ูˆุฐุฌ ุงู„ุงุชุตุงู„