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.
Run these examples in Google Colab or Kaggle Notebooks — free, nothing to install.
1. Load data
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
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 valuesdate 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
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
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
df["month"] = df["date"].dt.month
df["amount_with_gst"] = df["amount"] * 1.185. Group and summarise
df.groupby("city")["amount"].sum().sort_values(ascending=False)
df.groupby(["city", "month"]).agg(total=("amount", "sum"), orders=("amount", "count"))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
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
df.groupby("month")["amount"].sum().plot(kind="bar", title="Sales by month")Exercises
Make a small table yourself:
df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena", "Kiran"], "dept": ["Sales", "IT", "IT", "HR"], "salary": [42000, 65000, 72000, 38000]})- Show only people in IT.
- Average salary per department.
- Add a column "yearly" = salary × 12.
- Sort by salary, highest first.
- Who earns more than the average?
Answers
df[df["dept"] == "IT"]df.groupby("dept")["salary"].mean()df["yearly"] = df["salary"] * 12df.sort_values("salary", ascending=False)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.