pandas Selecting Examples
Short, runnable programs that show pandas selecting data in action. Read the explanation, press Run, then change the code and try your own ideas. New to the topic? Start with the pandas Selecting Data lesson.
1Series or DataFrame? One bracket versus two
A single name returns a Series; a list of names returns a DataFrame, even a list of one. The order of the list becomes the order of the columns.
import pandas as pd
students = pd.DataFrame({
"name": ["Asha", "Ben", "Chen", "Dina"],
"city": ["Pune", "Leeds", "Lima", "Oslo"],
"score": [88, 72, 95, 64],
})
one = students["score"]
many = students[["name", "score"]]
print(type(one).__name__, one.shape)
print(type(many).__name__, many.shape)
print(type(students[["score"]]).__name__)
print(students[["score", "name"]].head(2))
print("Average score:", one.mean())Expected outputSeries (4,)
DataFrame (4, 2)
DataFrame
score name
0 88 Asha
1 72 Ben
Average score: 79.75
2.loc with labels: read, slice and update
With product codes as the index, .loc reads cells by name. Slices include the last label, a mask can choose the rows, and assigning through .loc updates the DataFrame in place.
import pandas as pd
stock = pd.DataFrame({
"product": ["Tea", "Coffee", "Cocoa", "Sugar", "Flour"],
"price": [3.5, 6.0, 4.25, 1.5, 2.0],
"qty": [40, 25, 0, 100, 60],
}, index=["T1", "C1", "K1", "S1", "F1"])
print(stock.loc["C1", "product"])
print(stock.loc["T1":"K1", "price"])
print(stock.loc[stock["qty"] > 30, ["product", "qty"]])
stock.loc["K1", "qty"] = 50
stock.loc[stock["price"] < 2.5, "price"] = 2.5
print(stock[["price", "qty"]])Expected outputCoffee
T1 3.50
C1 6.00
K1 4.25
Name: price, dtype: float64
product qty
T1 Tea 40
S1 Sugar 100
F1 Flour 60
price qty
T1 3.50 40
C1 6.00 25
K1 4.25 50
S1 2.50 100
F1 2.50 60
3.iloc with positions, and how it differs from .loc
.iloc ignores the labels completely. Its slices stop before the end, while the same numbers used with .loc are labels and include the end.
import pandas as pd
log = pd.DataFrame({
"day": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
"steps": [8200, 10400, 7600, 12100, 9800, 15300],
"sleep": [7.5, 6.0, 8.0, 6.5, 7.0, 9.0],
}, index=[101, 102, 103, 104, 105, 106])
print(log.iloc[0, 1])
print(log.iloc[-1]["day"])
print(log.iloc[1:3, 1:])
print(log.loc[101:103, "steps"].tolist())
print(log.iloc[::2]["day"].tolist())
print(log.iloc[[0, 3, 5], [0, 1]])Expected output8200
Sat
steps sleep
102 10400 6.0
103 7600 8.0
[8200, 10400, 7600]
['Mon', 'Wed', 'Fri']
day steps
101 Mon 8200
104 Thu 12100
106 Sat 15300
4Filtering with isin, between, str methods and query
Each helper returns a True/False per row. ~ negates a condition, and query() writes the same kind of rule as a string. Notice that the filtered rows keep their original index labels.
import pandas as pd
orders = pd.DataFrame({
"customer": ["Ana Ruiz", "Raj Patel", "Li Wei", "Ana Costa", "Sam Lee", "Rita Rao"],
"city": ["Lisbon", "Delhi", "Lisbon", "Porto", "Delhi", "Pune"],
"amount": [42.0, 15.5, 88.0, 12.0, 60.0, 33.5],
"status": ["paid", "paid", "refunded", "paid", "pending", "paid"],
})
print(orders[orders["city"].isin(["Delhi", "Porto"])]["customer"].tolist())
print(orders[orders["amount"].between(30, 60)]["customer"].tolist())
print(orders[orders["customer"].str.startswith("Ana")]["amount"].tolist())
print(orders[orders["customer"].str.contains("ra", case=False)]["customer"].tolist())
print(orders[~(orders["status"] == "paid")][["customer", "status"]])
print(orders.query("amount > 30 and status == 'paid'")["customer"].tolist())Expected output['Raj Patel', 'Ana Costa', 'Sam Lee']
['Ana Ruiz', 'Sam Lee', 'Rita Rao']
[42.0, 12.0]
['Raj Patel', 'Rita Rao']
customer status
2 Li Wei refunded
4 Sam Lee pending
['Ana Ruiz', 'Rita Rao']
5Sorting by several columns and picking the top rows
ascending can be a list that matches the sort columns, so points can run high to low while minutes break ties from low to high. nlargest, nsmallest, head and tail pick rows from the ends.
import pandas as pd
players = pd.DataFrame({
"player": ["Maya", "Noah", "Omar", "Priya", "Quinn", "Ravi"],
"team": ["red", "blue", "red", "blue", "red", "blue"],
"points": [31, 27, 31, 18, 22, 27],
"minutes": [34, 30, 28, 22, 31, 25],
})
ranked = players.sort_values(["points", "minutes"], ascending=[False, True])
print(ranked[["player", "points", "minutes"]])
print("Most minutes:", players.nlargest(3, "minutes")["player"].tolist())
print("Fewest minutes:", players.nsmallest(2, "minutes")["player"].tolist())
print("Top two:", ranked.head(2)["player"].tolist())
print("Last two rows:", players.tail(2)["player"].tolist())Expected output player points minutes
2 Omar 31 28
0 Maya 31 34
5 Ravi 27 25
1 Noah 27 30
4 Quinn 22 31
3 Priya 18 22
Most minutes: ['Maya', 'Quinn', 'Noah']
Fewest minutes: ['Priya', 'Ravi']
Top two: ['Omar', 'Maya']
Last two rows: ['Quinn', 'Ravi']
6Deriving, dropping and renaming in one chain
assign() returns a new DataFrame, so the steps can be chained, and the original sales stays untouched. A final plain assignment adds one more column to the finished report.
import pandas as pd
sales = pd.DataFrame({
"item": ["Mug", "Poster", "Sticker", "Shirt"],
"units": [30, 12, 150, 20],
"unit_price": [8.0, 15.0, 1.5, 22.0],
"unit_cost": [5.0, 6.0, 0.5, 14.0],
})
report = (
sales
.assign(revenue=lambda d: d["units"] * d["unit_price"])
.assign(profit=lambda d: d["revenue"] - d["units"] * d["unit_cost"])
.drop(columns=["unit_price", "unit_cost"])
.rename(columns={"units": "sold"})
)
print(report)
report["margin_pct"] = (report["profit"] / report["revenue"] * 100).round(1)
print(report.sort_values("margin_pct", ascending=False)[["item", "margin_pct"]])
print("Original columns:", sales.columns.tolist())Expected output item sold revenue profit
0 Mug 30 240.0 90.0
1 Poster 12 180.0 108.0
2 Sticker 150 225.0 150.0
3 Shirt 20 440.0 160.0
item margin_pct
2 Sticker 66.7
1 Poster 60.0
0 Mug 37.5
3 Shirt 36.4
Original columns: ['item', 'units', 'unit_price', 'unit_cost']
Practise next
pandas Selecting Data exercises → Back to the lesson
NumPy Indexing and Slicing examplesPandas Basics examplespandas Data Cleaning examplespandas groupby and Pivot Tables examples