AttributeError:只能将.str访问器与字符串值一起使用,该字符串值在pandas中使用np.object_ dtype - python

Str.replace方法返回属性错误。

dc_listings['price'].str.replace(',', '')
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

这是我的价格栏的前5行。

AttributeError:只能将.str访问器与字符串值一起使用,该字符串值在pandas中使用np.object_ dtype - python

此堆栈溢出thread recommends来检查我的列是否具有NAN值,但我列中的所有值都不是NAN。
AttributeError:只能将.str访问器与字符串值一起使用,该字符串值在pandas中使用np.object_ dtype - python

参考方案

由于错误状态,您只能将.str与字符串列一起使用,并且您具有float64。浮动字符中不会包含任何逗号,因此您实际上不会做任何事情,但是总的来说,您可以先进行转换:

dc_listings['price'].astype(str).str.replace...

例如:

In [18]: df
Out[18]:
          a         b         c         d         e
0  0.645821  0.152197  0.006956  0.600317  0.239679
1  0.865723  0.176842  0.226092  0.416990  0.290406
2  0.046243  0.931584  0.020109  0.374653  0.631048
3  0.544111  0.967388  0.526613  0.794931  0.066736
4  0.528742  0.670885  0.998077  0.293623  0.351879

In [19]: df['a'].astype(str).str.replace("5", " hi ")
Out[19]:
0    0.64 hi 8208 hi  hi 4779467
1          0.86 hi 7231174332336
2            0.04624337481411367
3       0. hi 44111244991 hi 194
4          0. hi 287421814241892
Name: a, dtype: object

AttributeError:模块“tensorflow”没有属性“reset_default_graph” - python

我已经安装了tensorflow版本r0.11。在我的文件名cartpole.py中,我导入了tensorflow: import tensorflow as tf 并使用它: tf.reset_default_graph() 尝试在PyCharm中运行我的项目时出现以下错误:in <module> tf.reset_default_graph(…

AttributeError:模块“ tensorflow”没有属性“ keras” - python

最初使用下面的代码加载CIFAR-10数据集。(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() 但是,将Keras安装到环境后,运行上述行会导致错误:>>> AttributeError: module 'tensorflow&…

AttributeError:模块“ attr”没有属性“ s” - python

>>> import attr >>> @attr.s ... class SmartClass(object): ... a=attr.ib() ... b=attr.ib() ... Traceback (most recent call last): File "<stdin>", l…

如何检查变量是否是python中的特定类? - python

我有一个变量“ myvar”,当我打印出它时type(myvar)输出为:<class 'my.object.kind'> 如果我有10个变量的列表,包括此类字符串和变量..如何构造if语句来检查列表“ mylist”中的对象是否为<type 'my.object.kind'>? python大…

AttributeError:“模块”对象没有属性“xfeatures2d” [Python / OpenCV 2.4] - python

这行:sift = cv2.xfeatures2d.SIFT_create() 返回错误:Traceback (most recent call last): File "C:/Python27/openCVskrypty/GUI/SOLUTION2.py", line 11, in <module> sift = cv2.x…