Pandas Series: str.isnumeric() function
Series-str.isnumeric() function
The str.isnumeric() function is used to check whether all characters in each string are numeric or not.
This is equivalent to running the Python string method str.isnumeric() for each element of the Series/Index. If a string has zero characters, False is returned for that check.
Syntax:
Series.str.isnumeric(self)
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(['one', 'two2', '3', ''])
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(['one', 'two2', '3', ''])
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(['one', 'two2', '3', ''])
s1.str.isalnum()
Output:
0 True 1 True 2 True 3 False dtype: bool
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', '3,000'])
s2.str.isalnum()
Output:
0 False 1 False 2 False dtype: bool
Example - More Detailed Checks for Numeric Characters:
There are several different but overlapping sets of numeric characters that can be checked for.
Python-Pandas Code:
import numpy as np
import pandas as pd
s3 = pd.Series(['25', '³', '⅕', ''])
Example - The s3.str.isdecimal method checks for characters used to form numbers in base 10:
Python-Pandas Code:
import numpy as np
import pandas as pd
s3 = pd.Series(['25', '³', '⅕', ''])
s3.str.isdecimal()
Output:
0 True 1 False 2 False 3 False dtype: bool
Example - The s.str.isdigit method is the same as s3.str.isdecimal but also includes special digits, like superscripted and subscripted digits in unicode:
Python-Pandas Code:
import numpy as np
import pandas as pd
s3 = pd.Series(['25', '³', '⅕', ''])
s3.str.isdigit()
Output:
0 True 1 True 2 False 3 False dtype: bool
Example - The s.str.isnumeric method is the same as s3.str.isdigit but also includes other characters that can represent quantities such as unicode fractions:
Python-Pandas Code:
import numpy as np
import pandas as pd
s3 = pd.Series(['25', '³', '⅕', ''])
s3.str.isnumeric()
Output:
0 True 1 True 2 True 3 False dtype: bool
Previous: Series-str.istitle() function
Next: Series-str.isdecimal() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-str-isnumeric.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics