w3resource

Pandas Series: str.isalnum() function

Series-str.isalnum() function

The str.isalnum() function is used to check whether all characters in each string are alphanumeric or not.
This is equivalent to running the Python string method str.isalnum() for each element of the Series/Index. If a string has zero characters, False is returned for that check.

Syntax:

Series.str.isalnum(self)
Pandas Series: str.isalnum() function

Returns: Series or Index of bool
Series or Index of boolean values with the same length as the original Series/Index.

Example - Checks for Alphabetic and Numeric Characters:

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series(['two', 'two2', '2', ''])
s1.str.isalpha()

Output:

0     True
1    False
2    False
3    False
dtype: bool

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series(['two', 'two2', '2', ''])
s1.str.isnumeric()

Output:

0    False
1    False
2     True
3    False
dtype: bool

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series(['two', 'two2', '2', ''])
s1.str.isalnum()

Output:

0     True
1     True
2     True
3    False
dtype: bool
Pandas Series: str.isalnum() function

Note that checks against characters mixed with any additional punctuation or whitespace will evaluate to false for an alphanumeric check.

Python-Pandas Code:

import numpy as np
import pandas as pd
s2 = pd.Series(['P Q', '2.5', '2,000'])
s2.str.isalnum()

Output:

0    False
1    False
2    False
dtype: bool
Pandas Series: str.isalnum() function

Previous: Series-str.zfill() function
Next: Series-str.isalpha() function



Follow us on Facebook and Twitter for latest update.