python - 如何检查Python数据帧列中的第一个字符是否是数字

标签 python dataframe

我在 Python 数据框中有一些地址信息,我想检查其中一列的第一个字符是否是数字。我发现了类似的查询,但数据框中没有任何内容。我可以使用以下方法毫无问题地提取第一个字符:

check = df['ADDRESS_LINE_1'].str[0]

但是如果我尝试

check = df['ADDRESS_LINE_1'].str[0].isdigit()

我收到错误

'Series' object has no attribute 'isdigit'

我找不到等效的代码来让它在数据框中工作。我还尝试创建一个包含检查信息的新列,但这会导致相同的错误。如有任何帮助,我们将不胜感激。

最佳答案

您需要链接另一个str:

check = df['ADDRESS_LINE_1'].str[0].str.isdigit()
                                   #^ here

示例:

In [127]:
s = pd.Series(['3asd', 'asd', '3423', 'a123'])
s.str[0].str.isdigit()

Out[127]:
0     True
1    False
2     True
3    False
dtype: bool

您可以使用正则表达式来专门确定第一个字符是否为数字:

In [128]:
s.str.match('^\d')

Out[128]:
0     True
1    False
2     True
3    False
dtype: bool

该错误告诉您 isdigit 不是 Series 的方法,str.isdigit

关于python - 如何检查Python数据帧列中的第一个字符是否是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43145023/

相关文章:

python - 有没有办法使用索引和列名作为键从 Pandas 数据框中创建键值对字典?

python - 如何将行追加到 dictwriter python

python - 删除换行返回

python - 在数据框python的每一行中按字母顺序对单词进行排序

python-3.x - 无法用 map 替换 pandas 数据框列中的值

python - Pandas 在对行进行操作后重置索引

python - 如何修复数据帧的 "index out of bounds"错误?

python - 将 Keras 集成到 SKLearn 管道?

python - 获取openwrt中的物理接口(interface)列表

python - 使用 Selenium 选择具有特定内容的 anchor