Python | Pandas Series.str.isalpha()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
str.isalpha() method is used to check if all characters in each string in series are alphabetic(a-z/A-Z). Whitespace or any other character occurrence in the string would return false, but if there is a complete numeric value, then it would return NaN.
Return Type: Boolean series, Null values might be included too depending upon caller series.
To download the CSV used in code, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
In this example, the isalpha() method is applied on the College column. Before that, the Null rows are removed using .dropna() method to avoid errors.
As shown in the output image, the bool_series can be matched with the College column and it can be clearly seen that if the string contains only alphabets, True is returned.
In this example, the isalpha() method is applied on Name column twice. First a bool series is created for the original name column, after that the white spaces are removed using str.replace() method and then a new bool_series is created again.
As shown in the output image, the Bool series was false for all values until the strings had whitespace. After removing white spaces, the bool series in only false where the string is having special characters.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course