Filtering
Filtering rows with a condition
A comparison on a column produces True/False for every row. Put it in brackets to keep only the True rows:
Python
import pandas as pd
people = pd.DataFrame({
"name": ["Asha", "Leo", "Mei", "Raj", "Sofia"],
"age": [21, 30, 24, 35, 28],
"city": ["Mumbai", "London", "Osaka", "Delhi", "Rome"],
})
print(people["age"] > 25) # a boolean Series
print(people[people["age"] > 25]) # rows where it is TrueCombining conditions
Use & (and), | (or) and ~ (not), with brackets around each condition:
Python
import pandas as pd
people = pd.DataFrame({
"name": ["Asha", "Leo", "Mei", "Raj", "Sofia"],
"age": [21, 30, 24, 35, 28],
"country": ["India", "UK", "Japan", "India", "Italy"],
})
print(people[(people["age"] > 22) & (people["country"] == "India")])
print(people[(people["age"] < 22) | (people["age"] > 32)])isin and string methods
Python
import pandas as pd
people = pd.DataFrame({
"name": ["Asha Verma", "Leo Kim", "Mei Tanaka", "Raj Patel"],
"country": ["India", "UK", "Japan", "India"],
})
print(people[people["country"].isin(["India", "Japan"])])
print(people[people["name"].str.contains("a ")])
print(people[people["name"].str.startswith("M")]).str gives a column all the Python string methods, applied to every row.
query(): conditions as text
Python
import pandas as pd
products = pd.DataFrame({"name": ["Mouse", "Monitor", "Lamp", "Chair"], "price": [24.5, 279, 27.75, 349], "stock": [120, 15, 48, 8]})
print(products.query("price < 100 and stock > 20"))loc and iloc
locselects by labels (row index labels and column names),ilocselects by integer positions.
Python
import pandas as pd
df = pd.DataFrame({"name": ["Asha", "Leo", "Mei"], "age": [21, 30, 24], "city": ["Mumbai", "London", "Osaka"]})
print(df.loc[0, "name"]) # row label 0, column "name"
print(df.loc[df["age"] > 22, ["name", "city"]]) # filter rows AND pick columns
print(df.iloc[1]) # second row
print(df.iloc[:2, :2]) # first 2 rows, first 2 columnsdf.loc[condition, columns] is the clearest way to filter and select in one step.
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.