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.

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai