. Advertisement .
..3..
. Advertisement .
..4..
Hi everyone, I’m learning about python. While working, I try undefined. As a result, I get the message:
dc_listings['price'].str.replace(',', '')
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
What can I do about the “can only use .str accessor with string values, which use np.object_ dtype in pandas” issue? Is there a better approach?
The cause:
You get this error because you are trying to change a pattern in a string column, but the column that you are working with is not really a string.
Solution:
You can only use
.str
with string columns, and you have afloat64
, as the error shows. What you have will not really do anything because a float won’t contain any commas, but generally speaking, you can ignore it first:Let’s consider the following suggestion to solve your problem:
There are two ways to do it:
This error can be fixed with
series
.If
series
fails to work, you can useapply(str)
instead.