Data & BI 10 min read

Python for Data Analysis: A Pandas Crash Course for Analysts, with Runnable Code

This is a hands-on pandas walkthrough on one small sales dataset. Every snippet runs as written, and every output shown is what pandas actually printed.

Python pandas code for cleaning and summarising a sales dataset
Quick answer: For data analysis in Python, most analysts use the pandas library to do eight things: load a file with read_csv, inspect it with info and describe, clean it, filter rows, summarise with groupby, combine tables with merge, reshape with pivot_table and plot the result. If you already know Excel or SQL, two to three weeks of regular practice is a realistic estimate to get comfortable with them. The runnable example below takes a 12-row orders file from messy to a clean city and category summary.

Why do analysts use pandas for data analysis in Python?

Pandas gives you a table object, the DataFrame, that behaves like a spreadsheet you control with code. Anything you do in Excel with filters, VLOOKUP and PivotTables, you can do in pandas in a few lines, and then rerun on next month's file without clicking again.

That repeatability is the main reason analyst job posts ask for Python. SQL gets data out of a database. Pandas is what many analysts use next to clean it, join it with a CSV from another team, and produce a summary or chart. If you are still choosing what to learn first, our data analyst course syllabus guide shows where Python sits after Excel and SQL.

What do you need to install?

You have two easy options:

  • Google Colab: runs in the browser with pandas and matplotlib already available. Nothing to install, which suits a first week of practice.
  • Local install: install Python, then run pip install pandas matplotlib and work in Jupyter or VS Code. The pandas installation page lists the supported Python versions.

The code below was run with pandas 2.3 and also checked with the pandas 3.0 string and copy-on-write behaviour switched on. The only visible difference is that text columns show as str instead of object in info().

What sample dataset does this tutorial use?

Save the text below as orders.csv. It is a made-up set of 12 orders from four Indian cities, with the problems real exports usually have: inconsistent city names, a missing quantity and one duplicated row.

order_id,order_date,customer_id,city,category,quantity,unit_price
1001,2026-07-02,C01,Mumbai,Electronics,1,15000
1002,2026-07-03,C02,pune ,Fashion,3,800
1003,2026-07-05,C03,Delhi,Home,2,1200
1004,2026-07-05,C01,Mumbai,Fashion,2,950
1005,2026-07-08,C04,Bengaluru,Electronics,1,22000
1006,2026-07-10,C05,Delhi,Fashion,,700
1007,2026-07-12,C02,Pune,Home,4,450
1007,2026-07-12,C02,Pune,Home,4,450
1008,2026-08-01,C06,Bengaluru,Fashion,5,600
1009,2026-08-03,C03,Delhi,Electronics,1,18000
1010,2026-08-07,C04,bengaluru,Home,2,1500
1011,2026-08-09,C07,Mumbai,Home,1,2500

Save this second file as customers.csv. Notice that customer C07 is missing on purpose.

customer_id,customer_name,segment
C01,Asha Traders,Business
C02,R. Kulkarni,Consumer
C03,Nair Stores,Business
C04,S. Iyer,Consumer
C05,M. Khan,Consumer
C06,Patel & Sons,Business

Step 1: How do you load a CSV file in pandas?

import pandas as pd

pd.set_option("display.max_columns", None)
pd.set_option("display.width", 120)

orders = pd.read_csv("orders.csv")
customers = pd.read_csv("customers.csv")
print(orders.head())
   order_id  order_date customer_id       city     category  quantity  unit_price
0      1001  2026-07-02         C01     Mumbai  Electronics       1.0       15000
1      1002  2026-07-03         C02      pune       Fashion       3.0         800
2      1003  2026-07-05         C03      Delhi         Home       2.0        1200
3      1004  2026-07-05         C01     Mumbai      Fashion       2.0         950
4      1005  2026-07-08         C04  Bengaluru  Electronics       1.0       22000

Quantity shows as 1.0, not 1. That is your first clue: pandas stores a whole-number column as a decimal (float) when it contains a blank.

For Excel files use pd.read_excel("file.xlsx", sheet_name="Sheet1"), which needs the openpyxl package.

Step 2: How do you inspect a DataFrame before analysing it?

Spend two minutes here on every new file. These four checks catch most problems:

print(orders.shape)                 # (rows, columns)
orders.info()                       # column types and non-null counts
print(orders.isna().sum())          # blanks per column
print(orders.duplicated().sum())    # fully duplicated rows
print(orders["city"].value_counts())

What they tell you on this file:

  • shape prints (12, 7): 12 rows, 7 columns.
  • info() shows quantity has 11 non-null values out of 12, and order_date is stored as text, not a date.
  • isna().sum() shows 1 missing value, in quantity.
  • duplicated().sum() prints 1: order 1007 appears twice.
  • value_counts() shows six city values for four real cities:
city
Mumbai       3
Delhi        3
Bengaluru    2
Pune         2
pune         1
bengaluru    1
Name: count, dtype: int64

If you grouped by city now, Pune and Bengaluru would each be split in two. This is the most common silent error in beginner analysis.

Step 3: How do you clean data with pandas?

orders = orders.drop_duplicates()
orders["city"] = orders["city"].str.strip().str.title()
orders["quantity"] = orders["quantity"].fillna(1).astype(int)
orders["order_date"] = pd.to_datetime(orders["order_date"])
orders["revenue"] = orders["quantity"] * orders["unit_price"]
orders["month"] = orders["order_date"].dt.strftime("%Y-%m")

print(orders.shape)
print(orders["city"].value_counts())
(11, 9)
city
Mumbai       3
Delhi        3
Bengaluru    3
Pune         2
Name: count, dtype: int64

What each line does:

  • drop_duplicates() removes the repeated order 1007, leaving 11 rows.
  • str.strip().str.title() removes the trailing space in "pune " and fixes the capitals.
  • fillna(1) assumes a missing quantity means one unit. That is a business assumption, not a pandas rule. In real work, confirm it with the data owner or drop the row, and write the decision down.
  • pd.to_datetime turns text into real dates so you can extract months and weeks.
  • The last two lines add a revenue column and a month label like 2026-07.

A quick describe() after cleaning is a good sanity check:

print(orders[["quantity", "unit_price", "revenue"]].describe().round(1))
       quantity  unit_price  revenue
count      11.0        11.0     11.0
mean        2.1      5790.9   6609.1
std         1.4      8225.9   7717.7
min         1.0       450.0    700.0
25%         1.0       750.0   2150.0
50%         2.0      1200.0   2500.0
75%         2.5      8750.0   9000.0
max         5.0     22000.0  22000.0

The mean revenue per order (₹6,609) is far above the median (₹2,500). A few large electronics orders pull the average up, so the median is the fairer "typical order" here.

Explore your next step

Practise pandas on a larger, messier dataset

The ISS Data & Business Intelligence program page lists a Python week (Colab, pandas and NumPy on an e-commerce orders dataset) followed by a data cleaning and EDA week. Compare that with what you have covered here, and grab the free Data Analyst Starter Kit offered on this page for project ideas.

View Data & Business Intelligence curriculum →

Step 4: How do you filter rows in pandas?

Filtering uses a true/false condition inside square brackets. Wrap each condition in brackets and join them with & (and) or | (or). Python's and/or keywords do not work here.

delhi_big = orders[(orders["city"] == "Delhi") & (orders["revenue"] > 1000)]
print(delhi_big[["order_id", "category", "revenue"]])
   order_id     category  revenue
2      1003         Home     2400
9      1009  Electronics    18000

Delhi's third order (1006, ₹700) is correctly left out. Two other styles are worth knowing:

# rows whose category is in a list
home_fashion = orders[orders["category"].isin(["Home", "Fashion"])]
print(home_fashion.shape)          # (8, 9)

# the same kind of filter written as a query string
print(orders.query("revenue >= 5000")[["order_id", "city", "revenue"]])
   order_id       city  revenue
0      1001     Mumbai    15000
4      1005  Bengaluru    22000
9      1009      Delhi    18000

Step 5: How do you summarise data with groupby?

groupby is the pandas version of SQL's GROUP BY or a PivotTable with one row field. Named aggregations keep the output readable:

city_summary = (
    orders.groupby("city")
    .agg(orders=("order_id", "count"),
         revenue=("revenue", "sum"),
         avg_order=("revenue", "mean"))
    .sort_values("revenue", ascending=False)
    .round(0)
)
print(city_summary)
           orders  revenue  avg_order
city
Bengaluru       3    28000     9333.0
Delhi           3    21100     7033.0
Mumbai          3    19400     6467.0
Pune            2     4200     2100.0

Had you skipped the cleaning step, Bengaluru would show only ₹25,000 across two orders and would still rank first, but Pune would appear twice. Always clean category labels before grouping.

For a month-on-month view:

monthly = orders.groupby("month")["revenue"].sum()
print(monthly)
print(monthly.pct_change().round(3))
month
2026-07    46200
2026-08    26500
Name: revenue, dtype: int64
month
2026-07      NaN
2026-08   -0.426
Name: revenue, dtype: float64

August revenue fell 42.6%. Before reporting that, check coverage: August in this file only has orders up to the 9th. A partial month is one of the easiest ways to draw a wrong conclusion.

Step 6: How do you merge two tables in pandas?

merge works like a SQL join or an Excel XLOOKUP. Use how="left" to keep every order even when the customer is missing:

merged = orders.merge(customers, on="customer_id", how="left")
print(merged[["order_id", "customer_id", "customer_name", "segment"]].tail(4))
print(merged["segment"].isna().sum())
    order_id customer_id customer_name   segment
7       1008         C06  Patel & Sons  Business
8       1009         C03   Nair Stores  Business
9       1010         C04       S. Iyer  Consumer
10      1011         C07           NaN       NaN
1

Order 1011 has no match because C07 is not in the customer file. An inner join (how="inner") would have dropped it and quietly lost ₹2,500 of revenue. Label unmatched rows instead:

merged["segment"] = merged["segment"].fillna("Unknown")
print(merged.groupby("segment")["revenue"].sum())
segment
Business    40300
Consumer    29900
Unknown      2500
Name: revenue, dtype: int64

A good habit: compare row counts before and after every merge. If the count goes up, your key is duplicated in the right-hand table. Our guide to SQL joins explains the same logic in SQL.

Step 7: How do you make a pivot table in pandas?

pivot = merged.pivot_table(
    index="category",
    columns="month",
    values="revenue",
    aggfunc="sum",
    fill_value=0,
    margins=True,
    margins_name="Total",
)
print(pivot)
month        2026-07  2026-08  Total
category
Electronics    37000    18000  55000
Fashion         5000     3000   8000
Home            4200     5500   9700
Total          46200    26500  72700

This is the same table an Excel PivotTable would give you, with category in rows, month in columns and grand totals. Electronics is 76% of revenue (₹55,000 of ₹72,700) from just four orders, which is the kind of finding worth one sentence in a report.

Step 8: How do you plot results from pandas?

Pandas plots through matplotlib. One line gives a usable first chart:

import matplotlib.pyplot as plt

ax = city_summary["revenue"].plot(kind="bar", title="Revenue by city (Jul-Aug 2026)")
ax.set_ylabel("Revenue (Rs)")
plt.tight_layout()
plt.savefig("revenue_by_city.png")
plt.show()

Use a bar chart for comparing categories and a line chart (kind="line") for trends over time. For a stakeholder-facing dashboard, many teams move the cleaned data into a BI tool; see our Power BI projects for beginners.

Finally, save the clean table so you, or a BI tool, can reuse it:

merged.to_csv("orders_clean.csv", index=False)

Pandas cheat sheet: Excel and SQL equivalents

TaskPandasExcelSQL
Load datapd.read_csv()Open file / Power QueryTable already in database
See first rowsdf.head()ScrollSELECT * ... LIMIT 5
Count blanksdf.isna().sum()COUNTBLANKCOUNT(*) - COUNT(col)
Remove duplicatesdf.drop_duplicates()Remove DuplicatesSELECT DISTINCT
Filterdf[df["city"] == "Delhi"]FilterWHERE city = 'Delhi'
Summarisedf.groupby("city").agg(...)PivotTable / SUMIFSGROUP BY city
Lookup / joindf.merge(other, how="left")XLOOKUPLEFT JOIN
Cross-tabdf.pivot_table()PivotTableCASE WHEN + GROUP BY
Sortdf.sort_values("revenue")SortORDER BY revenue

What mistakes do beginners make with pandas?

  • Grouping before cleaning labels. "Pune" and "pune " become two cities.
  • Using and instead of & in filters, or forgetting the brackets around each condition. Both raise errors.
  • Inner joins by default. merge is inner unless you say otherwise, so unmatched rows vanish.
  • Reporting a mean for skewed data. Check the median, as in Step 3.
  • Comparing a partial month with a full one. Check the date range with df["order_date"].min() and .max().
  • Editing a filtered slice. If you want to change a filtered subset, take a copy first: subset = df[mask].copy().

How should you practise pandas after this crash course?

Pick one public dataset, such as an e-commerce, ride or sales dataset from Kaggle or a government open-data portal, and repeat the eight steps above. Then write three findings in plain English with one chart each. That notebook, cleaned up, is a portfolio piece; our guide to data analyst resume projects shows how to frame it.

Expect interviewers to ask you to explain your cleaning choices, not just show code. Practise saying why you filled a blank or dropped a row. Our data analytics interview questions guide covers how that reasoning is tested.

Frequently Asked Questions

Is pandas enough for Python data analysis?

For most analyst work, pandas plus matplotlib covers loading, cleaning, summarising and charting. You will also meet NumPy for numeric work and, later, libraries such as seaborn or scikit-learn. Start with pandas because almost everything else builds on it.

Should I learn SQL or pandas first?

Most analysts learn SQL first because company data usually sits in databases and SQL is tested in most analyst interviews. Pandas comes next, for cleaning files, combining sources and repeatable analysis. The ideas, such as filters, group-bys and joins, carry over directly.

How long does it take to learn pandas for a data analyst job?

As an estimate, someone who already knows Excel or SQL can get comfortable with the eight steps in this guide in two to three weeks of regular practice. Real fluency comes from repeating them on several messy public datasets.

Can I use pandas without installing anything?

Yes. Google Colab runs Python notebooks in the browser with pandas and matplotlib available, so you can follow this tutorial without a local setup.

What is the difference between groupby and pivot_table in pandas?

Both summarise data. groupby is the general tool and returns one row per group. pivot_table is a convenient wrapper that spreads one grouping across columns, like an Excel PivotTable, and can add totals with margins=True.

Do I need to know Python programming before learning pandas?

You need the basics: variables, lists, dictionaries, functions and how to import a library. A week of Python fundamentals is usually enough to start pandas, and you will pick up the rest as you go.

Sources and methodology

All code in this guide was run on the sample files shown, using pandas 2.3.3 with matplotlib, and re-run with pandas 3.0 behaviour enabled (future.infer_string and copy-on-write). The outputs are copied from those runs. Documentation was checked in September 2026.

The sample data is invented for teaching. The learning-time estimate is ISS editorial guidance, not a measured figure.

Next steps

Run the eight steps on a dataset you care about, then write up three findings. If you want structured practice with feedback, the Data & Business Intelligence curriculum covers SQL, Python with pandas, data cleaning and dashboards in a live 16-week cohort.

Applying is free, and you pay only after accepting an offer. Apply here, or download the free Data Analyst Starter Kit offered on this page for project briefs.

Get practical Python and SQL exercises for analysts

Occasional emails with small datasets, practice problems and study plans for data careers in India.