Resources / Data & AI
Free to readData & AI⏱ 2 min read

Python for data — pandas in one sitting

Load, clean, filter, group and chart data with pandas. A practical start for data analyst and data science roles.

Who this is for

You know a little Python (variables, lists, loops). Now you want to work with data the way analysts do. pandas is the Python library for tables — think Excel, but with code.

💡 Tip

Run these examples in Google Colab or Kaggle Notebooks — free, nothing to install.

1. Load data

📖 pandas DataFrame

Definition: A DataFrame is pandas' two-dimensional, labelled, tabular data structure with rows (an index) and named columns of possibly different types.

How it works: Each column is a Series. Operations are vectorised (applied to whole columns at once, fast). Key operations: selection, boolean filtering, groupby-aggregate, merge (join), pivot, handling missing values.

Example: df.groupby("city")["amount"].sum() gives revenue per city.

🧒 In simple words

Python
import pandas as pd
df = pd.read_csv("sales.csv")
df.head()        # first 5 rows
df.shape         # (rows, columns)
df.info()        # column types and missing values
▶ Output
   date        city       amount
0  2024-06-01  Pune       450
1  2024-06-01  Hyderabad  320
…
(1200, 3)   ← df.shape: 1,200 rows, 3 columns

A DataFrame (df) is a table. A single column is a Series.

2. Pick columns and rows

Python
df["city"]                         # one column
df[["city", "amount"]]             # several columns
df[df["amount"] > 1000]            # rows where amount > 1000
df[(df["city"] == "Pune") & (df["amount"] > 500)]

Use & (and) and | (or), with brackets around each condition.

3. Clean the data

Python
df.isna().sum()                    # missing values per column
df = df.dropna(subset=["amount"])  # drop rows with no amount
df["city"] = df["city"].str.strip().str.title()
df["date"] = pd.to_datetime(df["date"])
df = df.drop_duplicates()

Real data is messy. Cleaning is usually 60% of an analyst's work.

4. New columns

Python
df["month"] = df["date"].dt.month
df["amount_with_gst"] = df["amount"] * 1.18

5. Group and summarise

Python
df.groupby("city")["amount"].sum().sort_values(ascending=False)
df.groupby(["city", "month"]).agg(total=("amount", "sum"), orders=("amount", "count"))
▶ Output
city
Hyderabad    182400
Pune         154300
Chennai       98700
Name: amount, dtype: int64

This is the same idea as SQL's GROUP BY — see our SQL tutorial.

6. Combine tables

Python
merged = orders.merge(customers, on="customer_id", how="left")

how can be "inner", "left", "right" or "outer" — exactly like SQL joins.

7. A quick chart

Python
df.groupby("month")["amount"].sum().plot(kind="bar", title="Sales by month")

Exercises

Make a small table yourself:

Python
df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena", "Kiran"], "dept": ["Sales", "IT", "IT", "HR"], "salary": [42000, 65000, 72000, 38000]})
  1. Show only people in IT.
  2. Average salary per department.
  3. Add a column "yearly" = salary × 12.
  4. Sort by salary, highest first.
  5. Who earns more than the average?

Answers

  1. df[df["dept"] == "IT"]
  2. df.groupby("dept")["salary"].mean()
  3. df["yearly"] = df["salary"] * 12
  4. df.sort_values("salary", ascending=False)
  5. df[df["salary"] > df["salary"].mean()]

Your first portfolio project

Download a public dataset (sales, cricket, movies), ask 5 questions, answer them with pandas and 3 charts, write 5 lines of insight. Put the notebook on GitHub and the link on your resume.

Further reading

🔥Join the free 30-Day Placement ChallengeOne resource and one task a day — from resume to offer.