Python Tutorial › Libraries & Databases › Pandas

Python Pandas

IntermediateLesson 46 of 4715 min read12 runnable examples

pandas turns tables of data into DataFrames that you can filter, sort, group and summarise in a few lines. It reads CSV, Excel, JSON and SQL, which makes it the everyday tool for data analysis and for preparing test data.

Advertisement
In this lesson
  • Create Series and DataFrames
  • Read CSV files and inspect the data
  • Select, filter and sort rows
  • Handle missing values, group and merge

Install and import

Terminal
python -m pip install pandas
Python
import pandas as pdprint(int(pd.__version__.split(".")[0]) >= 2)
Output
True
Note

The examples were run with pandas 3. The in-browser Try it uses an older pandas, so a few outputs (such as column types) may print slightly differently there.

Series and DataFrame

A Series is one labelled column. A DataFrame is a table of Series that share an index.

Python
import pandas as pd scores = pd.Series([91, 78, 85], index=["Asha", "Ravi", "Meena"])print(scores)print(scores["Ravi"], scores.mean().round(2))
Output
Asha     91
Ravi     78
Meena    85
dtype: int64
78 84.67
Python
import pandas as pd df = pd.DataFrame({    "name": ["Asha", "Ravi", "Meena", "John", "Sara"],    "dept": ["QA", "Dev", "QA", "Ops", "Dev"],    "salary": [52000, 61000, 48000, 55000, 67000],    "years": [4, 6, 2, 5, 8],})print(df)print(df.shape)
Output
    name dept  salary  years
0   Asha   QA   52000      4
1   Ravi  Dev   61000      6
2  Meena   QA   48000      2
3   John  Ops   55000      5
4   Sara  Dev   67000      8
(5, 4)

Read a CSV file

Python
import pandas as pd with open("orders.csv", "w") as f:    f.write("order_id,city,amount,status\n101,Pune,1200,paid\n102,Delhi,450,pending\n"            "103,Pune,980,paid\n104,Mumbai,,cancelled\n105,Delhi,2300,paid\n") orders = pd.read_csv("orders.csv")print(orders.head(3))print(orders.dtypes["amount"], len(orders))
Output
   order_id   city  amount   status
0       101   Pune  1200.0     paid
1       102  Delhi   450.0  pending
2       103   Pune   980.0     paid
float64 5

Quick ways to inspect a new dataset:

Python
import pandas as pd orders = pd.read_csv("orders.csv")print(orders.columns.tolist())print(orders["amount"].describe().round(1).to_dict())print(orders["city"].value_counts().to_dict())
Output
['order_id', 'city', 'amount', 'status']
{'count': 4.0, 'mean': 1232.5, 'std': 778.2, 'min': 450.0, '25%': 847.5, '50%': 1090.0, '75%': 1475.0, 'max': 2300.0}
{'Pune': 2, 'Delhi': 2, 'Mumbai': 1}

Select columns and rows

Python
import pandas as pd df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena"], "dept": ["QA", "Dev", "QA"], "salary": [52000, 61000, 48000]}) print(df["name"].tolist())             # one columnprint(df[["name", "salary"]])          # several columnsprint(df.loc[1, "name"])               # by labelprint(df.iloc[0].to_dict())            # by position
Output
['Asha', 'Ravi', 'Meena']
    name  salary
0   Asha   52000
1   Ravi   61000
2  Meena   48000
Ravi
{'name': 'Asha', 'dept': 'QA', 'salary': 52000}

Filter rows

Python
import pandas as pd df = pd.DataFrame({    "name": ["Asha", "Ravi", "Meena", "John", "Sara"],    "dept": ["QA", "Dev", "QA", "Ops", "Dev"],    "salary": [52000, 61000, 48000, 55000, 67000],})print(df[df["salary"] > 55000])print(df[(df["dept"] == "QA") & (df["salary"] > 50000)]["name"].tolist())print(df[df["dept"].isin(["Dev", "Ops"])].shape[0])print(df.query("salary < 50000")["name"].tolist())
Output
   name dept  salary
1  Ravi  Dev   61000
4  Sara  Dev   67000
['Asha']
3
['Meena']

Sort and add columns

Python
import pandas as pd df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena"], "salary": [52000, 61000, 48000], "years": [4, 6, 2]})df["bonus"] = df["salary"] * 0.1df["level"] = df["years"].apply(lambda y: "senior" if y >= 5 else "junior")print(df.sort_values("salary", ascending=False))
Output
    name  salary  years   bonus   level
1   Ravi   61000      6  6100.0  senior
0   Asha   52000      4  5200.0  junior
2  Meena   48000      2  4800.0  junior

Missing data

Python
import pandas as pd orders = pd.read_csv("orders.csv")print(orders["amount"].isna().sum())print(orders.dropna(subset=["amount"]).shape)filled = orders.fillna({"amount": 0})print(filled["amount"].tolist())
Output
1
(4, 4)
[1200.0, 450.0, 980.0, 0.0, 2300.0]

Group and aggregate

groupby splits the rows into groups, applies a calculation to each, and combines the results:

Python
import pandas as pd orders = pd.read_csv("orders.csv")paid = orders[orders["status"] == "paid"]print(paid.groupby("city")["amount"].sum())print(orders.groupby("status").size().to_dict())print(paid.groupby("city")["amount"].agg(["count", "mean"]))
Output
city
Delhi    2300.0
Pune     2180.0
Name: amount, dtype: float64
{'cancelled': 1, 'paid': 3, 'pending': 1}
       count    mean
city                
Delhi      1  2300.0
Pune       2  1090.0

Merge two tables

Python
import pandas as pd users = pd.DataFrame({"user_id": [1, 2, 3], "name": ["Asha", "Ravi", "Meena"]})orders = pd.DataFrame({"order_id": [10, 11, 12, 13], "user_id": [1, 1, 3, 4], "amount": [500, 250, 900, 100]}) joined = orders.merge(users, on="user_id", how="left")print(joined)print(joined.groupby("name")["amount"].sum().to_dict())
Output
   order_id  user_id  amount   name
0        10        1     500   Asha
1        11        1     250   Asha
2        12        3     900  Meena
3        13        4     100    NaN
{'Asha': 750, 'Meena': 900}

A left merge keeps every order; user 4 has no match, so name is missing (NaN) for that row.

Save results

Python
import pandas as pd orders = pd.read_csv("orders.csv")summary = orders.groupby("city", as_index=False)["amount"].sum()summary.to_csv("city_totals.csv", index=False)print(open("city_totals.csv").read().strip())
Output
city,amount
Delhi,2750.0
Mumbai,0.0
Pune,2180.0

Check your understanding

Predict the output

import pandas as pddf = pd.DataFrame({"x": [1, 2, 3, 4], "g": ["a", "b", "a", "b"]})print(df[df["x"] > 1]["x"].sum(), df.groupby("g")["x"].sum().to_dict())

Quick quiz

1. Which reads a CSV file into a DataFrame?

2. What is the difference between loc and iloc?

3. How do you count missing values in a column?

Exercise: department report

For each department, print the number of employees and the average salary, sorted by average salary, highest first.

Input
the employee DataFrame from the Series and DataFrame section
Expected output
      count     mean
dept                
Dev       2  64000.0
Ops       1  55000.0
QA        2  50000.0
Hint 1

groupby("dept")["salary"].agg(["count", "mean"]), then sort_values("mean", ascending=False).

Program Practice

95 Python Pandas Programs

Every program has a step-by-step explanation, colour-coded code and its real output.

Start practising →
Related sets:All practice sets

Pandas FAQ

What is pandas used for in Python?

Loading, cleaning, transforming and summarising tabular data from CSV, Excel, JSON and SQL sources.

What is the difference between a Series and a DataFrame?

A Series is a single labelled column. A DataFrame is a two-dimensional table made of several Series that share an index.

How do I filter rows in pandas?

Use a boolean condition inside brackets: df[df["salary"] > 50000]. Combine conditions with & and | and wrap each one in brackets.

How do I handle missing values in pandas?

Find them with isna(), remove them with dropna(), or replace them with fillna().

2 thoughts on “Python Pandas”

Leave a Comment