# Complex demo: Load CSV, clean missing/duplicates/outliers, visualize with Pandas/Matplotlib
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def advanced_clean_csv(file):
    df = pd.read_csv(file)
    # Clean missing with multiple methods
    df['Value'].fillna(df['Value'].mean(), inplace=True)
    df['Category'].fillna('Unknown', inplace=True)
    # Remove duplicates with subset
    df.drop_duplicates(subset=['ID'], keep='first', inplace=True)
    # Detect outliers with IQR
    Q1 = df['Value'].quantile(0.25)
    Q3 = df['Value'].quantile(0.75)
    IQR = Q3 - Q1
    df = df[~((df['Value'] < (Q1 - 1.5 * IQR)) | (df['Value'] > (Q3 + 1.5 * IQR)))]
    # Add log column
    df['Log Value'] = np.log(df['Value'] + 1)
    return df

def visualize_cleaned(df):
    fig, ax = plt.subplots(figsize=(10, 6))
    ax.scatter(df['ID'], df['Log Value'], c='blue', marker='o', label='Log Value')
    ax.set_title('Cleaned CSV Log Values', fontsize=16)
    ax.set_xlabel('ID', fontsize=12)
    ax.set_ylabel('Log Value', fontsize=12)
    ax.legend()
    ax.annotate('High Log', xy=(df['ID'].iloc[df['Log Value'].argmax()], df['Log Value'].max()), xytext=(df['ID'].iloc[df['Log Value'].argmax()] + 0.5, df['Log Value'].max() - 0.5), arrowprops=dict(facecolor='black', shrink=0.05))
    plt.show()

# Assume csv4.csv with ID, Value, Category columns (some missing/duplicates/outliers)
cleaned = advanced_clean_csv("csv4.csv")
visualize_cleaned(cleaned)
print("Cleaned DataFrame:\n", cleaned)