# Pandas Data Analysis Tool

This guide demonstrates how to build a command-line tool for analyzing datasets using `pandas`. It reads a CSV file (e.g., sales records), calculates key metrics like total revenue and best-selling products, and exports a summary to a new file.

**Modules Used:**
*   [[programming/python/modules/pandas-module|pandas]]: For high-performance data manipulation and analysis.
*   [[programming/python/modules/argparse-module|argparse]]: To handle input and output file arguments.
*   [[programming/python/modules/openpyxl-module|openpyxl]]: Required if exporting to Excel (`.xlsx`).

## Installation

```bash
pip install pandas openpyxl
```

## The Code

Save this as `analyzer.py`.

```python
import pandas as pd
import argparse
import sys
import os

def analyze_data(input_file, output_file):
    if not os.path.exists(input_file):
        print(f"Error: Input file '{input_file}' not found.")
        return

    try:
        # 1. Load Data
        print(f"Loading data from {input_file}...")
        df = pd.read_csv(input_file)
        
        # Basic Validation: Ensure required columns exist
        required_columns = ['Product', 'Category', 'Price', 'Quantity', 'Date']
        missing_cols = [col for col in required_columns if col not in df.columns]
        if missing_cols:
            print(f"Error: Input CSV is missing columns: {', '.join(missing_cols)}")
            return

        # 2. Data Cleaning / Preparation
        # Calculate 'Total' sales for each row
        df['Total'] = df['Price'] * df['Quantity']
        
        # Convert Date column to datetime objects for accurate sorting/filtering
        df['Date'] = pd.to_datetime(df['Date'])

        # 3. Perform Analysis
        print("-" * 30)
        print("DATA SUMMARY")
        print("-" * 30)
        
        total_revenue = df['Total'].sum()
        print(f"Total Revenue: ${total_revenue:,.2f}")
        
        # Find the product with the highest total sales
        top_product = df.groupby('Product')['Total'].sum().idxmax()
        print(f"Best Selling Product: {top_product}")
        
        # Group by Category to see performance
        category_summary = df.groupby('Category')[['Quantity', 'Total']].sum().reset_index()
        print("\nSales by Category:")
        print(category_summary)

        # 4. Export Results
        if output_file:
            print(f"\nSaving summary to {output_file}...")
            if output_file.endswith('.csv'):
                category_summary.to_csv(output_file, index=False)
            elif output_file.endswith('.xlsx'):
                category_summary.to_excel(output_file, index=False)
            else:
                # Default to CSV if extension is unknown
                print("Unknown output format. Saving as CSV.")
                category_summary.to_csv(output_file + ".csv", index=False)
            print("Done.")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Sales Data Analysis Tool")
    parser.add_argument("input", help="Path to input CSV file")
    parser.add_argument("-o", "--output", help="Path to output summary file (CSV or Excel)")
    
    args = parser.parse_args()
    
    analyze_data(args.input, args.output)
```

## Usage

1.  **Create a dummy data file** named `sales.csv`:
    ```csv
    Product,Category,Price,Quantity,Date
    Laptop,Electronics,1200,5,2023-01-01
    Mouse,Electronics,25,50,2023-01-02
    Coffee,Food,5,100,2023-01-03
    Desk,Furniture,150,10,2023-01-04
    Monitor,Electronics,300,15,2023-01-05
    ```

2.  **Run the tool:**
    ```bash
    # Analyze and print to console
    python analyzer.py sales.csv

    # Analyze and save summary to Excel
    python analyzer.py sales.csv --output summary.xlsx
    ```

[[programming/python/python]]