Links

Filter DataFrame

Tags: #pandas #dataframe #filter #python #dataanalysis
Author: Florent Ravenel
Description: This notebook will show how to filter a DataFrame using Pandas.
References:

Input

Import libraries

import pandas as pd

Model

Create DataFrame

# Create a fake dataset
data = {
"name": ["Jason", "Molly", "Tina", "Jake", "Amy"],
"year": [2012, 2012, 2013, 2014, 2014],
"reports": [4, 24, 31, 2, 3],
}
# Create a DataFrame
df = pd.DataFrame(data)
df

Filter DataFrame

  • Comparison Operators: You can use comparison operators (>, <, >=, <=, ==, !=) to create filters based on specific conditions. For example, this filter will only show the rows where the number of reports is greater than 4.
  • isin() Method: You can use the isin() method to filter rows based on whether a value is present in a given list or array. For example, filtering rows where the 'category' column has values 'A' or 'B':
# Filter the DataFrame
df_filtered = df.copy()
df_filtered = df_filtered[df_filtered["reports"] > 4]
df_filtered

Output

Display result

# Display the filtered DataFrame
df_filtered