当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python pandas.Series.str.isdigit用法及代码示例


用法:

Series.str.isdigit()

检查每个字符串中的所有字符是否都是数字。

这相当于为 Series/Index 的每个元素运行 Python 字符串方法str.isdigit()。如果字符串有零个字符,则为该检查返回False

返回

布尔系列或索引

与原始系列/索引长度相同的布尔值系列或索引。

例子

检查字母和数字字符

>>> s1 = pd.Series(['one', 'one1', '1', ''])
>>> s1.str.isalpha()
0     True
1    False
2    False
3    False
dtype:bool
>>> s1.str.isnumeric()
0    False
1    False
2     True
3    False
dtype:bool
>>> s1.str.isalnum()
0     True
1     True
2     True
3    False
dtype:bool

请注意,对于混合了任何其他标点符号或空格的字符的检查将评估为 false 以进行字母数字检查。

>>> s2 = pd.Series(['A B', '1.5', '3,000'])
>>> s2.str.isalnum()
0    False
1    False
2    False
dtype:bool

更详细的数字字符检查

可以检查几个不同但重叠的数字字符集。

>>> s3 = pd.Series(['23', '³', '⅕', ''])

s3.str.isdecimal 方法检查用于生成以 10 为底的数字的字符。

>>> s3.str.isdecimal()
0     True
1    False
2    False
3    False
dtype:bool

s.str.isdigit 方法与 s3.str.isdecimal 相同,但也包括特殊数字,如 unicode 中的上标和下标数字。

>>> s3.str.isdigit()
0     True
1     True
2    False
3    False
dtype:bool

s.str.isnumeric 方法与 s3.str.isdigit 相同,但还包括其他可以表示数量的字符,例如 unicode 分数。

>>> s3.str.isnumeric()
0     True
1     True
2     True
3    False
dtype:bool

检查空格

>>> s4 = pd.Series([' ', '\t\r\n ', ''])
>>> s4.str.isspace()
0     True
1     True
2    False
dtype:bool

检查字符大小写

>>> s5 = pd.Series(['leopard', 'Golden Eagle', 'SNAKE', ''])
>>> s5.str.islower()
0     True
1    False
2    False
3    False
dtype:bool
>>> s5.str.isupper()
0    False
1    False
2     True
3    False
dtype:bool

s5.str.istitle 方法检查所有单词是否都大写(是否只有每个单词的首字母大写)。单词被假定为由空白字符分隔的任何非数字字符序列。

>>> s5.str.istitle()
0    False
1     True
2    False
3    False
dtype:bool

相关用法


注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.Series.str.isdigit。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。