83

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.

In [122]: dc_listings['price'].head()
Out[122]:
577     185.0
2166    180.0
3631    175.0
71      128.0
1011    115.0
Name: price, dtype: float64

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

In [130]: dc_listings[dc_listings['price'].isnull()]
Out[130]:
 host_response_rate host_acceptance_rate ... price cleaning_fee security_deposit
1
  • 4
    Your column is a float, so you can't use the string methods on it. Also, since it's a float column, it won't have ',' in it. Commented Aug 28, 2018 at 20:44

5 Answers 5

165

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
Sign up to request clarification or add additional context in comments.

Comments

19

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(',', '')
    

Comments

0

Randy has the solution to handle your problem with changing the whole column into str type. But when you have non-str-type value (like NA, list, dict, a custom class) inside that column and wanted to filter those special values in the future, i suggest you create your own function and then apply it to the str value only, like this:

dc_listings['price'] = dc_listings['price'].apply(
   lambda x: x.replace(',', '') if type(x) is str else x
)

or more clearly, using def :

def replace_substring_or_return_value(value):
  if type(value) is str: return x.replace(',', '')
  else: return value

dc_listings['price'] = dc_listings['price'].apply(
      replace_substring_or_return_value
)

ALTHOUGH this might be a bad practice, because you should use the same data type for every value in a column

Comments

-1

first step is convert a numerical values (like int,float) to string values (like object) after you can perform a function.

For example...

df['column_name'] = df['column_name'].astype("int/float").astype('str')

After this step you can perform a any string functions...

1 Comment

.astype("int/float") ???
-2

If price is a dtype float 64 then the data is not a string. You can try dc_listings['price'].apply(function)

1 Comment

What's function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.