Excel Apply Autofilter and Customfilter using Python

What will you learn?

In this tutorial, you will master the art of applying autofilter and custom filters in Excel using Python. By leveraging Python libraries like pandas and openpyxl, you’ll be able to efficiently filter data based on specific criteria, automating the process and enhancing your data manipulation skills.

Introduction to the Problem and Solution

Working with large datasets in Excel often requires filtering out specific data based on certain conditions. In this tutorial, we delve into programmatically manipulating Excel data by applying filters using Python. By harnessing the power of pandas and openpyxl, we can streamline our workflow, handle large datasets effectively, and automate the filtering process.


# Import necessary libraries
import pandas as pd

# Load the Excel file into a DataFrame
df = pd.read_excel('input.xlsx')

# Apply Autofilter on a specific column (e.g., 'Column1')
filtered_data = df[df['Column1'] > 50]

# Create Custom Filter - Filtering rows where 'Column2' contains 'Value'
custom_filtered_data = df[df['Column2'].str.contains('Value')]

# Save the filtered data to a new Excel file
filtered_data.to_excel('output_autofiltered.xlsx', index=False)
custom_filtered_data.to_excel('output_customfiltered.xlsx', index=False)

# Visit our website for more Python tutorials: PythonHelpDesk.com

# Copyright PHD


  • Importing Libraries: Utilize pandas library for efficient data manipulation.
  • Loading Data: Read Excel file into a Pandas DataFrame for processing.
  • Applying Autofilter: Filter rows based on a specified condition in a particular column.
  • Creating Custom Filter: Implement custom filters based on text values.
  • Saving Filtered Data: Save filtered results to new Excel files without including the index.
    How do I install pandas library?

    To install pandas library, use pip:

    pip install pandas    
    # Copyright PHD

    Can I apply multiple filters at once?

    Yes, you can chain multiple conditions while filtering in pandas DataFrame.

    Is it possible to apply advanced filtering techniques?

    Pandas supports complex filtering operations by combining logical operators like AND (&) or OR (|).

    Can I filter based on date ranges?

    Yes, you can filter date columns by specifying date ranges or conditions.

    How do I remove duplicate rows after applying filters?

    Use .drop_duplicates() method in Pandas DataFrame to remove duplicate rows.

    Is it possible to filter based on NULL or NaN values?

    Pandas offers functions like .isnull() or .notnull() for handling missing values during filtering.


    Filtering data in an Excel spreadsheet is crucial when dealing with extensive datasets. Automating this process through Python not only saves time but also ensures accuracy and reproducibility. By mastering these techniques, you enhance your data manipulation skills significantly. For additional resources and tutorials visit Python Help Desk.

    Leave a Comment