简体   繁体   English

Python AttributeError:“系列”对象没有属性“ isdigit”

[英]Python AttributeError: 'Series' object has no attribute 'isdigit'

I am trying replace number to '' blank if the row/column contains numbers. 如果行/列包含数字,我正在尝试将数字替换为''空白。 I tried the following it keeps complaing isdigit doesn't exist? 我尝试了以下方法,它一直抱怨isdigit不存在? I tried converting the column to string and doesn't help. 我尝试将列转换为字符串,但无济于事。 Is there other operators that I could use for the pandas data frame? 我还有其他运算符可用于熊猫数据框吗?

data = ['123567','1547892','2547879','ABC','3D']
df1 = pd.DataFrame(data)
df1.columns = ['col1']

df1.col1 = str(df1.col1)
if len(df1.col1) < 8 and df1.col1.isdigit(): # errors
    df1.col1 == ''
    print(df1)

Looking for an output like this: 寻找这样的输出:

col1
0   
1   
2   
3   ABC
4   3D

To access string methods on a series, you need to do so via the .str attribute of Series : 要访问序列中的字符串方法,您需要通过Series.str属性进行访问

df1.col1.str.isdigit()

See Series.str.isdigit() for the documentation. 有关文档,请参见Series.str.isdigit()

You can use that as a boolean index and directly assign to the selected rows: 您可以将其用作布尔索引,然后直接分配给选定的行:

df1.col1[df1.col1.str.isdigit()] = ''

See Working with Text Data . 请参阅使用文本数据

Do not use df1.col1.str.isdigit() in an if statement, because a boolean array is not True or False by itself, it is an array of boolean values and so would throw ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() 不要在if语句中使用df1.col1.str.isdigit() ,因为布尔数组本身不是True或False,它是布尔值的数组,因此会引发ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() if used in a boolean context. 如果在布尔上下文中ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Demo: 演示:

>>> import pandas as pd
>>> data = ['123567','1547892','2547879','ABC','3D']
>>> df1 = pd.DataFrame(data)
>>> df1.columns = ['col1']
>>> df1
      col1
0   123567
1  1547892
2  2547879
3      ABC
4       3D
>>> df1.col1[df1.col1.str.isdigit()] = ''
>>> df1
  col1
0
1
2
3  ABC
4   3D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-AttributeError:“ str”对象没有属性“ isDigit” - Python - AttributeError: 'str' object has no attribute 'isDigit' AttributeError:&#39;int&#39;对象没有属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' AttributeError: 'generator' object 没有属性 'isdigit' - AttributeError: 'generator' object has no attribute 'isdigit' AttributeError: &#39;int&#39; 对象没有属性 &#39;isdigit&#39; 和 NameError - AttributeError: 'int' object has no attribute 'isdigit' and NameError AttributeError:“列表”对象没有属性“ isdigit” - AttributeError: 'list' object has no attribute 'isdigit' 为什么我的代码不起作用 AttributeError: 'int' object has no attribute 'isdigit' - why my code not working AttributeError: 'int' object has no attribute 'isdigit' AttributeError:&#39;int&#39;对象没有用户输入的属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' from user input AttributeError&#39;int&#39;对象在Ubuntu中的ipython中没有属性isdigit - AttributeError 'int' object has no attribute isdigit in ipython in ubuntu python pandas- AttributeError:&#39;Series&#39;对象没有属性&#39;columns&#39;? - python pandas- AttributeError: 'Series' object has no attribute 'columns'? Python 错误使用 Panda AttributeError: 'Series' object has no attribute 'asType' - Python error using Panda AttributeError: 'Series' object has no attribute 'asType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM