How to Use the any() Method in Pandas


Often you may want to extract the rows from a pandas DataFrame in which any column in the row meets a specific condition.

The easiest way to do this is by using the any() method in pandas, which uses the following basic syntax:

df.any(axis=0, bool_only=False, skipna=True, …)

where:

  • axis: The axis to search over
  • bool_only: Whether or not to include Boolean columns
  • skipna: Whether or not to exclude NA and null values

The following example shows how to use the any() method in practice with a pandas DataFrame.

Example: How to Use any() Method in Pandas

Suppose we create the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

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

#view DataFrame
print(df)

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

The DataFrame contains the following information:

  • team: The team a player belongs to
  • points: Total points scored by the player
  • assists: Total assists by the player
  • rebounds: Total rebounds by the player

Suppose that we would like to extract all of the rows in which the value 10 occurs in any column.

We can use the following syntax to do so:

#extract all rows where a value of 10 occurs in any column
df[df.isin([10]).any(axis=1)]

        team	points	assists	rebounds
2	Heat	15	7	10

Note: We specified axis=1 within the any() function to specify that the columns should be searched over.

This returns one row.

We can see that this particular row contains a value of 10 in the rebounds column, which is why this particular row is returned.

Note that no other row in the DataFrame contained a value of 10 in any column.

Note that we can use similar syntax to return all rows that contain one of several numeric values.

For example, we can use the following syntax to extract all rows that contain a value of 10 or 12 in any column:

#extract all rows where a value of 10 or 12 occurs in any column
df[df.isin([10, 12]).any(axis=1)]

	team	points	assists	rebounds
1	Mavs	12	7	8
2	Heat	15	7	10
4	Kings	19	12	6

We can see that this returns three rows, all of which contain the value of either 10 or 12 in at least one of their columns.

Note that we specified two values within the pandas isin() function but you can include as many values as you would like.

Also note that we can specify string values to search for as well.

For example, we can use the following syntax to return all rows in the DataFrame that contain the string “Heat” in any column:

#extract all rows where a value of "Heat" occurs in any column
df[df.isin(['Heat']).any(axis=1)]

	team	points	assists	rebounds
2	Heat	15	7	10
3	Heat	14	9	6

We can see that this returns the two rows that contain the string “Heat” in the team column.

Note: You can find the complete documentation for the any() method in pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

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

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *