3.3 C
London
Thursday, March 13, 2025
HomePandas in PythonDataFrame Functions in PythonPandas: Select Rows Where Value Appears in Any Column

Pandas: Select Rows Where Value Appears in Any Column

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Often you may want to select the rows of a pandas DataFrame in which a certain value appears in any of the columns.

Fortunately this is easy to do using the .any pandas function. This tutorial explains several examples of how to use this function in practice.

Example 1: Find Value in Any Column

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19],
                   'assists': [5, 7, 7, 9, 12],
                   'rebounds': [11, 8, 10, 6, 6]})

#view DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6

The following syntax shows how to select all rows of the DataFrame that contain the value 25 in any of the columns:

df[df.isin([25]).any(axis=1)]

        points	assists	rebounds
0	25	5	11

The following syntax shows how to select all rows of the DataFrame that contain the values 25, 9, or 6 in any of the columns:

df[df.isin([25, 9, 6]).any(axis=1)]

        points	assists	rebounds
0	25	5	11
3	14	9	6
4	19	12	6

Example 2: Find Character in Any Column

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19],
                   'assists': [5, 7, 7, 9, 12],
                   'position': ['G', 'G', 'F', 'F', 'C']})

#view DataFrame
print(df)

   points  assists position
0      25        5        G
1      12        7        G
2      15        7        F
3      14        9        F
4      19       12        C

The following syntax shows how to select all rows of the DataFrame that contain the character G in any of the columns:

df[df.isin(['G']).any(axis=1)]


points	assists	position
0	25	5	G
1	12	7	G

The following syntax shows how to select all rows of the DataFrame that contain the values G or C in any of the columns:

df[df.isin(['G', 'C']).any(axis=1)] 

points	assists	position
0	25	5	G
1	12	7	G
4	19	12	C

Additional Resources

How to Filter a Pandas DataFrame on Multiple Conditions
How to Find Unique Values in Multiple Columns in Pandas
How to Get Row Numbers in a Pandas DataFrame

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories