Sam Dillard
Sam Dillard

Reputation: 690

Apply str.replace() to string but error says it's a float

I'm using Pandas to strip a '$' out of some strings in a column.

df['Amount'].dtypes` --> `dtype('O')

So, I figure I can do string modification now:

df['Amount'].apply(lambda x: x.replace('$', ''))

I get the error: 'float' object has no attribute 'replace'.

Why would the type "Object" (string) but be "float" when I'm operating on it?

Upvotes: 1

Views: 192

Answers (1)

mozway
mozway

Reputation: 261850

Object doesn't mean string, it means not a homogeneous vectorizable dtype.

You can use:

df['Amount'].astype(str).str.replace('$', '')

Upvotes: 4

Related Questions