Practicle 2:-
Data Cleaning -noisy Data
1.COde
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def load_and_clean_data():
    try:
        df=pd.read_csv('/content/sample_data/noisy_data.csv',parse_dates=['date'])
        print('column detected succesfully')
    except:
        print('error finding file')
    df.columns=df.columns.str.strip()
    return df
print("Loading Data...")
df=load_and_clean_data()
print("\nOg Data (5 rows )")
print(df.head)
plt.figure(figsize=(6, 4))
plt.boxplot(df['price'])
plt.title("Boxplot - Product Prices (Before Cleaning)")
plt.ylabel("Price ($)")
plt.tight_layout()
plt.show()
print("\nConverting date format...")
df['date'] = pd.to_datetime(df['date'], format='%d-%m-%y')
print("\n Identifying and Removing Outliers..")
def remove_outliers(column):
    column=pd.to_numeric(column,errors='coerce')
    Q1=column.quantile(0.25)
    Q3=column.quantile(0.75)
    IQR=Q3-Q1
    lower=Q1-1.5*IQR
    upper=Q3+1.5*IQR
    print(f"\nColumn statistics:Q1={Q1},Q3={Q3},IQR={IQR}")
    print(f"Outlier bounds: lower={lower}, upper={upper}")
    return column.apply(lambda x: np.nan if (x < lower or x > upper) else x)
price_no_outliers = remove_outliers(df['price'])
quantity_no_outliers = remove_outliers(df['quantity'])
df = df.assign(price=price_no_outliers, quantity=quantity_no_outliers)
plt.figure(figsize=(6, 4))
plt.boxplot(df['price'].dropna()) # Modified this line to drop NaNs
plt.title("Boxplot - Product Prices (After Cleaning)")
plt.ylabel("Price ($)")
plt.tight_layout()
plt.show()
cleaned_file_path = "/content/sample_data/cleaned_sales_data.csv"
df.to_csv(cleaned_file_path, index=False)
print("\n✅ Cleaning complete!")
print(f"Cleaned data saved to: {cleaned_file_path}")
print(f"\nFinal cleaned data shape: {df.shape}")
print("\nPreview of Cleaned Data (first 5 rows):")
print(df.head())
Output:-
2.