Learn Data Analysis with Python

Python is the leading language for data analysis, thanks to powerful libraries like Pandas and Matplotlib.

Using Pandas to Analyze Data

Pandas is used for data manipulation and analysis. Here's how you can read a CSV file and get basic insights.

import pandas as pd

# Assume 'sales.csv' has columns: 'product', 'price', 'quantity'
try:
    # Load the dataset
    df = pd.read_csv('sales.csv')

    # Calculate total revenue
    df['revenue'] = df['price'] * df['quantity']

    # Find the top 5 best-selling products by revenue
    top_products = df.groupby('product')['revenue'].sum().nlargest(5)

    print("Top 5 Products by Revenue:")
    print(top_products)

except FileNotFoundError:
    print("Error: sales.csv not found. Please create this file to run the script.")

Interactive Python Playground

Test your Python data analysis scripts in the editor below.