← Pandas
Lesson 2 of 5
Filtering and sorting
Watch, read the card, then practise. Filtering rows · loc and iloc · Sorting
The video for this lesson is being recorded.
Meanwhile, the notes below and the 60-second cards cover the same ideas.
Practise this topicFiltering rows
Filter rows by putting a condition inside df[...].
Remember: Condition in brackets, & between them.
loc and iloc
loc picks rows and columns by label; iloc picks them by position.
Remember: loc = labels. iloc = integer positions.
Sorting
sort_values orders rows by one or more columns.
Remember: sort_values returns a new table; save it or use it straight away.
Try the example in Code Lab
import pandas as pd
df = pd.DataFrame({"name": ["Asha", "Ravi", "Meena"], "city": ["Pune", "Delhi", "Pune"], "marks": [82, 67, 91]})
print(df[(df["city"] == "Pune") & (df["marks"] > 85)]["name"].tolist())