Python | Pandas Series.str.isalpha ()

Python Methods and Functions

str.isalpha() Pandas str.isalpha() is used to check if all characters on each line, the series are alphabetic (az / AZ). Space or any other character occurrence in the string will return false, but if there is a full numeric value, then NaN will be returned.

Syntax: Series.str.isalpha ()

Return Type: Boolean series, Null values ​​might be included too depending upon caller series.

To load the CSV used in the code, click here.

In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below. 

Example # 1:
In this example the isalpha () method is applied to the College column. Before that, null lines are removed using the .dropna () method to avoid errors.

# pandas module import

import pandas as pd

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

 
# remove null values ​​to avoid errors

data.dropna (inplace = True )

 
# create bool series

data [ "bool_series" ] = data [ "College " ]. str . isalpha ()

 
# display
data

Output:
As shown in the output image, bool_series can be matched against the College column and you can clearly see that if the string contains alphabets only, True is returned. 

Example # 2:
In this example the isalpha () method is applied to the Name column twice. First, a bool series is created for the original name column, and then the spaces are removed using the str.replace (), and then a new bool_series is created again.

# pandas module import

import pandas as pd

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

  
# remove null values ​​to avoid errors

data.dropna ( inplace = True )

 
# create bool series with original column

data [ " bool_series1 " ] = data [ "Name" ]. str . isalpha ()

 
# remove spaces

data [ " Name " ] = data [ "Name" ]. str . replace ( " " , "")

 
# creating a bool series with a 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 ​​as long as the lines had no spaces. After removing spaces in the string, bool is only specified false if there are special characters in the string. 





Get Solution for free from DataCamp guru