Pandas DataFrame filter() Method – Be on the Right Side of Change (original) (raw)


Preparation

Before any data manipulation can occur, one (1) new library will require installation:

To install this library, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required library.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd

💡 Note: To follow along with the examples below, click here to download the finxters.csv file of auto-generated dummy user data. Move this file to the current working directory.


The filter() method returns a subset of a DataFrame/Series rows/columns based on index label(s). This method does not filter a DataFrame/Series on the contents. The filter applies to the specific label(s) of the selected index. The return value is a subset of the callable.

The syntax for this method is as follows:

DataFrame.filter(items=None, like=None, regex=None, axis=None)

Parameter Description
items A filter list of columns to include in the result.
like A filter string of columns (ex: like='FID') to include in the result.
regex A regex string to filter columns (ex: regex='e$') to include in the result.
axis If zero (0) or index is selected, apply to each column. Default 0.If one (1) apply to each row..

For this method, various scenarios below highlight its capabilities.

This example uses the items parameter to filter the DataFrame and return the result.

Code – Example 1

df_fxs = pd.read_csv('finxters.csv') result = df_fxs.filter(items=['Username', 'Rank']) print(result.head())

Output

| | Username | Rank | | | ---------- | ---------- | ------------------- | | 0 | wildone92 | Authority | | 1 | AmyP | Beginner | | 2 | 1998_pete | Basic Knowledge | | 3 | TheCoder | Experienced Learner | | 4 | AliceM | Authority |

This example uses the like parameter to filter the DataFrame and return the result.

Code – Example 2

df_fxs = pd.read_csv('finxters.csv') result = df_fxs.filter(like='FID', axis=1) print(result.head())

Output

| | FID | | | ----- | -------- | | 0 | 30022145 | | 1 | 30022192 | | 2 | 30022331 | | 3 | 30022345 | | 4 | 30022359 |

This example uses the regex parameter to filter the DataFrame and return the result.

Code – Example 3

df_fxs = pd.read_csv('finxters.csv') result = df_fxs.filter(regex='e$', axis=1) print(result.head())

Output

| | First_Name | Last_Name | Username | | | ------------- | ---------- | --------- | ---------- | | 0 | Steve | Hamilton | wildone92 | | 1 | Amy | Pullister | AmyP | | 2 | Peter | Dunn | 1998_pete | | 3 | Marcus | Williams | TheCoder | | 4 | Alice | Miller | AliceM |

Note: Each column name in the output ends with the letter e.


More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.