AttributeError:只能将.str访问器与字符串值一起使用,该字符串值在pandas中使用np.object_ dtype [英] AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

查看:278
本文介绍了AttributeError:只能将.str访问器与字符串值一起使用,该字符串值在pandas中使用np.object_ dtype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Str.replace method returns an attribute error.

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

这是我的价格列的前5行.

Here are the top 5 rows of my price column.

此堆栈溢出线程建议检查我的列中是否包含NAN值,但我列中的所有值都不是NAN.

This stack overflow thread recommends to check if my column has NAN values but non of the values in my column are NAN.

推荐答案

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

As the error states, you can only use .str with string columns, and you have a float64. There won't be any commas in a float, so what you have won't really do anything, but in general, you could cast it first:

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:只能将.str访问器与字符串值一起使用,该字符串值在pandas中使用np.object_ dtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆