Pandas DataFrames: Exercises


3. Filtering a Pandas DataFrame. In Pandas, you can filter a DataFrame to select rows that meet specific conditions. You can use boolean indexing to create filters and select rows based on criteria.

For example, you can filter students who are older than 20 like this: df[df['Age'] > 20].


# Import the Pandas library import pandas as pd # Create a Pandas DataFrame with student information df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [20, 22, 21]}) # Filter students who are older than 20 filtered_df = df[df['Age'] ___ 20] # Print the filtered DataFrame print(filtered_df) # Import the Pandas library import pandas as pd # Create a Pandas DataFrame with student information df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [20, 22, 21]}) # Filter students who are older than 20 filtered_df = df[df['Age'] > 20] # Print the filtered DataFrame print(filtered_df) test_object("filtered_df") success_msg("You have correctly filtered the Pandas DataFrame based on age.")
To filter a Pandas DataFrame, use boolean indexing to select rows that meet specific conditions.

Previous Exercise Next Exercise