[英]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.