How to Use the isalpha() Function in Pandas


Often you may want to check whether all characters in a given string in pandas are alphabetical.

The most efficient way to do so is by using the isalpha() function, which performs this exact task.

The isalpha() function uses the following syntax:

Series.str.isalpha()

Note that in practice you will replace “Series” with the name of your pandas Series.

This function will return True or False for each individual string in a Series to indicate whether or not the string contains only alphabetical characters.

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

Example: How to Use the isalpha() Function in Pandas

Suppose we create the following pandas Series that contains various strings:

import pandas as pd

#create pandas Series
my_series = pd.Series(['hello', 'five', '65 days', '19', '20 goats', 'Mike2', '44.3'])

#view pandas Series
print(my_series)

0       hello
1        five
2     65 days
3          19
4    20 goats
5       Mike2
6        44.3
dtype: object

Suppose that we would like to check if each string in the Series contains only alphabetical characters.

We can use the isalpha() function with the following syntax to do so:

#check if each string in Series contains only alphabetical characters
my_series.str.isalpha()

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

Notice that a value of either True or False is returned for each string in the Series to indicate whether or not the string contains only alphabetical characters.

For example, from the output we can see:

  • ‘hello’ contains only alphabetical characters, so this returns True.
  • ‘five’ contains only alphabetical characters, so this returns True.
  • ’65 days’ does not contain only alphabetical characters, so this returns False.
  • ’19’ does not contain only alphabetical characters, so this returns False.
  • ’20 goats’ does not contain only alphabetical characters, so this returns False.
  • ‘Mike2’ does not contain only alphabetical characters, so this returns False.
  • ‘44.3’ does not contain only alphabetical characters, so this returns False.

And so on.

Note that we can also use the islpaha() function to filter the values in a Series based on whether or not the strings contain only alphabetical characters.

For example, we can use the following syntax to filter the Series to only show the strings that contain only alphabetical characters:

#filter Series to only show strings that contain alphabetical characters
my_series[my_series.str.isalpha()]

0    hello
1     five
dtype: object

We can see that this returns only the strings in the Series that contain only alphabetical characters.

We can also perform the reverse operation and filter the Series to only show strings that do not contain only alphabetical characters:

#filter Series to only show strings that don't contain only alphabetical characters
my_series[~my_series.str.isalpha()]

2     65 days
3          19
4    20 goats
5       Mike2
6        44.3
dtype: object

We can see that this returns only the strings in the Series that do not contain only alphabetical characters.

Note that we used the tilde ( ~ ) operator as short-hand for “not” in pandas.

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

Additional Resources

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

How to Use asfreq() Function in Pandas
How to Use the nunique() Function in Pandas
How to Use the get_loc() Function in Pandas
How to Use idxmin() Function in Pandas

Featured Posts

Leave a Reply

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