How to Use the isnumeric() Function in Pandas


Often you may want to check if a particular string in pandas contains a number.

The most efficient way to do so is by using the str.isnumeric() function, which uses the following syntax:

pandas.Series.str.isnumeric()

This function will check if all characters in each string in a series or column of a pandas DataFrame are numeric and return either True or False as the result.

The following example shows how to use the str.isnumeric() function in practice with a pandas DataFrame.

Note: Make sure to use the syntax str.isnumeric() instead of just isnumeric(), which will result in an error. This is a common mistake to avoid.

Example: How to Use the isnumeric() Function 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': ['A', 'A', 'B', 'B', 'C', 'C', 'C'],
                   'points': [12, 18, 18, 22, 30, 41, 12],
                   'assists': [8, 10, 11, 11, 7, 12, 8],
                   'championships': ['2', 'zero', 'one', '3', 'zero', '5', '7']})

#view DataFrame
print(df)

  team  points  assists championships
0    A      12        8             2
1    A      18       10          zero
2    B      18       11           one
3    B      22       11             3
4    C      30        7          zero
5    C      41       12             5
6    C      12        8             7

The championships column in the DataFrame represents the number of championships that each player has won.

Note that all of the elements in this column are strings, but some of the strings contain letters while some contain only numbers.

Suppose that we would like to check if each strings in the column contains a particular number.

We can use the following syntax with the str.isnumeric() function to do so:

#check if each value in championships column contains the name of a particular number
df['championships'].str.isnumeric()

0     True
1    False
2    False
3     True
4    False
5     True
6     True
Name: championships, dtype: bool

The output displays either True or False for each row in the championships column.

Here is how to interpret the output:

  • The first row has a value of True because the string ‘2’ contains a number.
  • The second row has a value of False because the string ‘zero’ contains a string and not a number.
  • The third row has a value of False because the string ‘one’ contains a string and not a number.

And so on.

Note that the opposite of the str.isnumeric() function is the str.isalpha() function, which allows us to check if each string in a column contains only alphabetical characters.

We can use the following syntax to with the str.isalpha() function to check if each string in the championships column contains only alphabetical characters:

#check if each value in championships column contains only alphabetical characters
df['championships'].str.isalpha()

0    False
1     True
2     True
3    False
4     True
5    False
6    False
Name: championships, dtype: bool

Here is how to interpret the output:

  • The first row has a value of False because the string ‘2’ does not contain only alphabetical characters.
  • The second row has a value of True because the string ‘zero’ contains only alphabetical characters.
  • The third row has a value of True because the string ‘one’ contains only alphabetical characters.

And so on.

Note: You can find the complete documentation for the isnumeric() function in pandas here.

Additional Resources

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

How to Use the nunique() Function in Pandas
How to Use the get_loc() Function in Pandas
How to Create a Tuple from Two Columns in Pandas

Featured Posts

Leave a Reply

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