Skip to content
Related Articles

Related Articles

Python | Pandas Series.str.isalpha()
  • Last Updated : 23 Aug, 2019

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.

Pandas 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.

Syntax: Series.str.isalpha()

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.

 
Example #1:
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.




# importing pandas module
import pandas as pd
  
# making data frame
  
# removing null values to avoid errors
data.dropna(inplace = True)
  
# creating bool series
data["bool_series"]= data["College"].str.isalpha()
  
# display
data


Output:
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.

 
Example #2:
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.




# importing pandas module
import pandas as pd
  
# making data frame
  
# removing null values to avoid errors
data.dropna(inplace = True)
  
# creating bool series with original column
data["bool_series1"]= data["Name"].str.isalpha()
  
# removing white spaces
data["Name"]= data["Name"].str.replace(" ", "")
  
# creating bool series with new column
data["bool_series2"]= data["Name"].str.isalpha()
  
# display
data.head(10)


Output:
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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :