Change language

Python | Pandas Series.str.isalpha ()

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. 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically