Pandas for Analytics

    Yash Kabra3 min readUpdated

    Pandas is the Python library that turns messy data into clean, analysable tables called DataFrames. For analytics it is the workhorse: you load data, filter rows, group and aggregate, and combine tables — all in a few readable lines. This guide walks through the operations you will use every single day.

    The DataFrame

    A DataFrame is a table with labelled rows and columns, like a spreadsheet you control with code.

    import pandas as pd  # pandas 2.x
    
    df = pd.DataFrame({
        "product": ["Pen", "Notebook", "Pen", "Eraser"],
        "region":  ["North", "South", "South", "North"],
        "units":   [50, 30, 45, 20],
        "price":   [10, 40, 10, 5],
    })
    
    print(df.head())     # first rows
    print(df.info())     # column names, types, and non-null counts
    

    Selecting and filtering

    # Select a single column
    print(df["product"])
    
    # Filter rows with a condition
    print(df[df["units"] > 30])
    
    # Combine conditions with & (and) / | (or) — wrap each in parentheses
    print(df[(df["region"] == "North") & (df["units"] > 20)])
    

    Creating new columns

    # Revenue = units * price, calculated for every row at once
    df["revenue"] = df["units"] * df["price"]
    print(df)
    

    Grouping and aggregating

    This is the heart of analytics — summarising data by category.

    # Total revenue per region
    print(df.groupby("region")["revenue"].sum())
    
    # Multiple aggregations at once
    print(df.groupby("product").agg(
        total_units=("units", "sum"),
        avg_price=("price", "mean"),
    ))
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Combining tables

    When data lives in two tables, merge them on a shared key.

    import pandas as pd
    
    sales = pd.DataFrame({"product": ["Pen", "Notebook"], "units": [95, 30]})
    info  = pd.DataFrame({"product": ["Pen", "Notebook"], "category": ["Stationery", "Stationery"]})
    
    # Join the two tables on the 'product' column
    merged = sales.merge(info, on="product", how="left")
    print(merged)
    
    # To stack rows from two DataFrames, use concat — NOT the removed .append()
    more_sales = pd.DataFrame({"product": ["Eraser"], "units": [20]})
    combined = pd.concat([sales, more_sales], ignore_index=True)
    print(combined)
    

    Sorting and ranking

    # Sort by revenue, highest first
    print(df.sort_values("revenue", ascending=False))
    

    These few operations — filter, create, group, merge, sort — cover the majority of real analytics work. From here, data visualisation turns your summaries into charts.

    Common mistakes

    • Using df.append(). It was removed from pandas. Always use pd.concat([...], ignore_index=True) to stack rows.
    • Using .ix. This indexer was removed long ago. Use .loc for labels and .iloc for positions.
    • Forgetting parentheses in combined filters. df[(a) & (b)] is required; without parentheses Python misreads the operator precedence.
    • Chained assignment. Modifying a filtered copy may not change the original. Assign with .loc, e.g. df.loc[df["units"] > 30, "flag"] = True.

    FAQ

    How do I load a CSV? pd.read_csv("file.csv"). For Excel use pd.read_excel("file.xlsx").

    What is the difference between .loc and .iloc? .loc selects by label (column or index name); .iloc selects by integer position.

    Is pandas fast enough for large data? For most analytics tasks, yes. Very large datasets may need chunking or other tools, but you will get far with pandas first.

    Keep learning

    Pandas is the bridge between raw data and insight. Pair it with data cleaning and Python for data science, or explore the full Data Science & Analytics hub.

    Want guided practice with real datasets? Join the waitlist for the Data Science & Analytics course at Infoplanet, Jalgaon.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Yash Kabra

    Founder, Atlee Technologies

    Yash Kabra is the founder of Atlee Technologies, a product studio that ships SaaS products end-to-end. He owns products from strategy through launch and growth — including Infoplanet, TrackRise and Perqee — and teaches AI, Machine Learning and Data Science at Infoplanet with a focus on how these tools are used to build real products.

    Related guides