Change language

Python | Pandas dataframe.filter ()

| | |

The Pandas function dataframe.filter() is used to subset the rows or columns of data according to the labels in the specified index. Note that this procedure does not filter the data frame by its content. The filter is applied to index labels.

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

Parameters:
items: List of info axis to restrict to (must not all be present)
like: Keep info axis where “arg in col == True”
regex: Keep info axis with re.search (regex, col) == True
axis: The axis to filter on. By default this is the info axis, ’index’ for Series, ’columns’ for DataFrame

Returns: same type as input object

Items, like and regex parameters are mutually exclusive. the default axis is the information axis used when indexing with [].

To link to the CSV file, click here

Example # 1: Use the filter () function to filter out any three frame columns data.

# import pandas as pd

import pandas as pd

 
# Create a data frame

df = pd.read_csv ( "nba.csv" )

 
# Print the data frame df

Now filter out the Name, College and Salary columns.

# apply filter function

df. filter ([ "Name" , " College " , " Salary " ])

Output:

Example # 2: Use the filter () function to substitute all columns in a data frame whose name contains the letter "a" or "A".

Note: The filter () function also accepts a regular expression as one of its parameters.

# import pandas as pd

import pandas as pd

 
# Create a data frame

df = pd.read_csv ( " nba.csv " )

 
# Using a regular expression to extract all
# columns that have a or a in their name.

df. filter (regex < code class = "keyword"> = ’[aA]’ )

Output:

The regular expression" [aA] "searches for all column names that have" a "or" A "in their name.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method