0
0
Pandasdata~5 mins

Selecting rows by condition in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of selecting rows by condition in pandas?
Selecting rows by condition helps you filter data to keep only the rows that meet specific criteria, making it easier to analyze relevant information.
Click to reveal answer
beginner
How do you select rows where a column 'Age' is greater than 30 in a pandas DataFrame named df?
Use df[df['Age'] > 30] to get all rows where the 'Age' column value is greater than 30.
Click to reveal answer
beginner
What does the expression df[df['Score'] >= 80] return?
It returns a new DataFrame containing only the rows where the 'Score' column is 80 or higher.
Click to reveal answer
intermediate
How can you select rows where column 'City' is either 'New York' or 'Chicago'?
Use df[df['City'].isin(['New York', 'Chicago'])] to select rows where 'City' is one of the listed values.
Click to reveal answer
intermediate
What is the difference between using & and | operators in pandas conditions?
& means AND (both conditions must be true), | means OR (at least one condition must be true). Remember to use parentheses around each condition.
Click to reveal answer
Which code selects rows where 'Salary' is less than 50000?
Adf.loc['Salary' < 50000]
Bdf['Salary'] < 50000
Cdf[df['Salary'] < 50000]
Ddf['Salary'].filter(<50000)
How do you select rows where 'Age' is between 20 and 30 inclusive?
Adf[(df['Age'] >= 20) & (df['Age'] <= 30)]
Bdf[df['Age'] > 20 and df['Age'] < 30]
Cdf[df['Age'] >= 20 | df['Age'] <= 30]
Ddf[df['Age'].between(20, 30)]
What does df[df['Status'] == 'Active'] do?
ASelects rows where 'Status' is 'Active'
BSelects rows where 'Status' is not 'Active'
CChanges 'Status' to 'Active'
DDeletes rows where 'Status' is 'Active'
Which method helps select rows where a column matches any value in a list?
Afilter()
Bisin()
Cmatch()
Dcontains()
How do you combine two conditions to select rows where 'Age' > 25 OR 'Score' > 90?
Adf[(df['Age'] > 25) & (df['Score'] > 90)]
Bdf[df['Age'] > 25 and df['Score'] > 90]
Cdf[df['Age'] > 25 or df['Score'] > 90]
Ddf[(df['Age'] > 25) | (df['Score'] > 90)]
Explain how to select rows in a pandas DataFrame based on a condition involving a single column.
Think about how you write conditions like 'Age > 30' inside the brackets.
You got /3 concepts.
    Describe how to combine multiple conditions to filter rows in pandas.
    Remember to use parentheses and the right logical operators.
    You got /3 concepts.