Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

Tags:

python

pandas

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 

Here are the top 5 rows of my price column.

enter image description here

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

like image 864
MJP Avatar asked Aug 28 '18 20:08

MJP


People also ask

Can only use .STR accessor with string values in pandas?

The AttributeError: Can only use . str accessor with string values, which use np. object_ dtype in pandas occurs if you try to replace the values of string column, but in reality, it is of a different type. We can fix the issue by casting the column to a string before replacing the values in the column.

Can you only use .STR accessor with string value?

str accessor with string values! occurs when you try to use the string accessor attribute . str , but the Series contains values that are not strings. You can solve this by casting the Series to string using astype(str) .

Is Dtype object a string?

Moreover, having dtype as Object will make it less clear to work with just text and exclude the non-text values. With the new String dtype, the values are explicitly treated as strings.

How to fix the attributeerror can only use STR accessor in pandas?

The AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas occurs if you try to replace the values of string column, but in reality, it is of a different type. We can fix the issue by casting the column to a string before replacing the values in the column. Blogger, Traveller, Investor and Technologist.

Why doesn't the STR accessor work with non-string dtypes?

The problem here is that when you read your txt file, in it is casting "c" as an integer and the .str accessor will not work with non-string dtypes, you can fix this problem a couple of ways: Cast the integer as a string in the print statement. Thanks for contributing an answer to Stack Overflow!

Is it possible to replace a string with STR in pandas?

And yes df ['foo'] = df ['foo'].str.replace ('.', ',') worked with pandas. Can only use .str accessor with string values, which use np.object_ dtype in pandas


2 Answers

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... 

For example:

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 
like image 153
Randy Avatar answered Sep 20 '22 11:09

Randy


Two ways:

  1. You can use series to fix this error.

    dc_listings['price'].series.str.replace(',', '') 

  1. And if series doesn't work you can also alteratively use apply(str) as shown below:

    dc_listings['price'].apply(str).str.replace(',', '') 
like image 39
Mitushi Ananya Avatar answered Sep 20 '22 11:09

Mitushi Ananya