Pandas.DataFrame.filter() Method in Python

Pandas Dataframe filter() method is “used to filter the DataFame rows and columns.” The returned DataFrame contains only rows and columns specified by the function.

Syntax

DataFrame.filter(self: ~FrameOrSeries, items=None, like: Union[str, NoneType] = None, 
                 regex: Union[str, NoneType] = None, axis=None)

Parameters

items: list-like

Keep labels from the axis that are in items.

like: str

Keep labels from the axis for which “like in label == True”.

regex: str (regular expression)

Keep labels from the axis for which re.search(regex, label) == True.

axis: {0 or ‘index’, 1 or ‘columns’, None}, the default value is None.

The axis to filter on is expressed either as the index (int) or axis name (str).

By default, this is an info axis, ‘index’ for Series, and ‘columns’ for DataFrame.

Return value

It returns the same type as the input object.

Example 1: How to Use Pandas.DataFrame.filter() Method

import pandas as pd

data = {
  'A': [1, 2, 3],
  'B': [4, 5, 6],
  'C': [7, 8, 9]
}

df = pd.DataFrame(data)

# Filter columns using items
filtered_columns = df.filter(items=['A', 'C'])
print("Filtered columns using items:\n", filtered_columns)

# Filter columns using like
filtered_columns_like = df.filter(like='A')
print("\nFiltered columns using like:\n", filtered_columns_like)

# Filter columns using regex
filtered_columns_regex = df.filter(regex='[BC]')
print("\nFiltered columns using regex:\n", filtered_columns_regex)

# Filter rows using items
filtered_rows = df.filter(items=[0, 2], axis=0)
print("\nFiltered rows using items:\n", filtered_rows)

Output

Filtered columns using items:
  A  C
0 1  7
1 2  8
2 3  9

Filtered columns using like:
  A
0 1
1 2
2 3

Filtered columns using regex:
  B  C
0 4  7
1 5  8
2 6  9

Filtered rows using items:
  A  B  C
0 1  4  7
2 3  6  9

Example 2: Pandas.DataFrame.filter() Columns by Labels

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
  'A': [1, 2, 3],
  'B': [4, 5, 6],
  'C': [7, 8, 9]
})

# Use filter() to select columns 'A' and 'C'
filtered_df = df.filter(items=['A', 'C'])

print(filtered_df)

Output

   A  C
0  1  7
1  2  8
2  3  9

Example 3: Pandas.DataFrame.filter() Rows by Index

import pandas as pd

df = pd.DataFrame({
  'A': [1, 2, 3],
  'B': [4, 5, 6],
  'C': [7, 8, 9]
}, index=['row1', 'row2', 'row3'])

filtered_df = df.loc[['row1', 'row3']]

print(filtered_df)

Output

       A   B   C
row1   1   4   7
row3   3   6   9 

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.