LearnFrenzy
Sign in Take a free test

Python, Charts & Storytelling · Lesson 5 of 6

Python pandas for Data Analysis: Load, Explore, Group and Merge

pandas is Excel and SQL inside Python — with no row limit and every step saved as code. Learn the 15 operations that cover most analysis work, using a real-world style orders dataset.

13 min read Intermediate Updated Nov 9, 2026 Reviewed by Saurabh Samir

If Excel is a calculator and SQL is a question you ask a database, pandas is a full workshop. It loads data from CSV, Excel, databases and APIs, cleans and reshapes it, and every step is saved as code you can rerun next month in seconds. That repeatability is why analysts move to Python.

Load and look

import pandas as pd

orders = pd.read_csv("orders.csv", parse_dates=["order_date"])
print(orders.shape)          # (rows, columns)
print(orders.head())         # first five rows
orders.info()                # column types and missing values
print(orders.describe())     # count, mean, min, max, quartiles for numbers

Always start with these four. They answer "what does one row mean, how big is it, and what looks wrong?" before you calculate anything.

Select and filter

orders["city"]                                   # one column (a Series)
orders[["order_id", "city", "amount"]]          # several columns (a DataFrame)

pune_big = orders[(orders["city"] == "Pune") & (orders["amount"] > 20000)]
south    = orders[orders["city"].isin(["Chennai", "Bengaluru", "Hyderabad", "Kochi"])]
october  = orders[orders["order_date"].dt.month == 10]

Use & and | (not and / or) and put each condition in brackets — the most common beginner error in pandas.

New columns

orders["month"]      = orders["order_date"].dt.to_period("M")
orders["high_value"] = orders["amount"] >= 20000
orders["band"]       = pd.cut(orders["amount"], bins=[0, 5000, 20000, float("inf")],
                              labels=["Low", "Medium", "High"])

Group and summarise (pandas' GROUP BY)

summary = (orders.groupby("city")
                 .agg(orders=("order_id", "nunique"),
                      revenue=("amount", "sum"),
                      avg_order=("amount", "mean"))
                 .sort_values("revenue", ascending=False))
print(summary.head(10))

# Share of revenue by category
share = orders.groupby("category")["amount"].sum() / orders["amount"].sum() * 100
print(share.round(1))

Pivot tables in code

pivot = orders.pivot_table(index="city", columns="category", values="amount",
                           aggfunc="sum", fill_value=0, margins=True)

That is the Excel pivot from earlier — except it reruns on next month's file with no clicking.

Merge tables (pandas' JOIN)

customers = pd.read_csv("customers.csv")
full = orders.merge(customers, on="customer_id", how="left", validate="many_to_one")
print(len(orders), len(full))     # should be equal for a many-to-one left merge

Habit that prevents silent errors: compare row counts before and after every merge, and use validate=. If a "many_to_one" merge suddenly has more rows, the customer table has duplicate IDs — a data problem you must fix before trusting any total.

Time series in one line

monthly = orders.set_index("order_date").resample("MS")["amount"].sum()
growth  = monthly.pct_change() * 100          # month-over-month growth %

Quick charts

import matplotlib.pyplot as plt

monthly.plot(kind="line", title="Monthly revenue (₹)")
plt.tight_layout(); plt.savefig("monthly_revenue.png")

summary["revenue"].head(8).plot(kind="barh", title="Top cities by revenue")
plt.gca().invert_yaxis(); plt.tight_layout(); plt.savefig("top_cities.png")

Save your result

summary.to_csv("city_summary.csv")
pivot.to_excel("city_category_pivot.xlsx")     # needs the openpyxl package

pandas, SQL and Excel — the same ideas

TaskExcelSQLpandas
Filter rowsFilterWHEREdf[df["x"] > 5]
Group totalsPivot tableGROUP BYgroupby().agg()
Bring in columnsXLOOKUPJOINmerge()
Running totalFormulaSUM() OVERcumsum()

Learn the idea once, and you can use it in all three tools.

Try it yourself: In Google Colab (free, nothing to install), load any sales or e-commerce CSV and produce: revenue by month, top 5 categories, average order value by city, and one chart. Save the notebook to GitHub — it is your first analytics portfolio piece.

Common interview questions

Try answering each one aloud before you open it.

1How do you group data and calculate several aggregates in pandas?

Use groupby on the key column followed by agg with named aggregations, for example df.groupby("city").agg(revenue=("amount","sum"), orders=("order_id","nunique")).

2What is the difference between merge and concat?

merge joins two DataFrames side by side on key columns like a SQL JOIN. concat stacks DataFrames vertically (more rows) or horizontally along an axis without matching keys.

3Why use & instead of "and" when filtering?

Filters work element-wise on whole columns. "and" expects single True/False values, while & combines boolean Series row by row. Each condition must be in parentheses because of operator precedence.

Practise this

Build something with it

FAQs

Do I need Jupyter to use pandas?

No, but notebooks (Jupyter or Google Colab) are ideal for analysis because you see each result immediately. Use plain .py scripts for repeatable reports.

pandas or SQL — when do I use which?

Do the heavy filtering and joining in SQL close to the data, then bring the smaller result into pandas for cleaning, analysis and charts. Many analysts use both daily.

References & further reading

Finished this lesson?

Mark it complete. Create a free account to save it on every device.

Next lesson

Search LearnFrenzy

Esc

Quick links