Skip to content
Advertisement

Python Error: ‘float’ object has no attribute ‘replace’

I am an R User that is trying to learn more about Python.

I found this Python library that I would like to use for address parsing: https://github.com/zehengl/ez-address-parser

I was able to try an example over here:

JavaScript

I have the following file that I imported:

JavaScript

I then applied the above function and export the file and everything works:

JavaScript

Problem: I now have another file (similar format) – but this time, I am getting an error:

JavaScript

I am confused as to why the same code will not work for this file. As I am still learning Python, I am not sure where to begin to debug this problem. My guesses are that perhaps there are special characters in the second file, formatting issues or incorrect variable types that are preventing this ap.parse function from working, but I am still not sure.

Can someone please show me what to do?

Thank you!

Advertisement

Answer

Looking at the code from the library, we have this method for parse in the AddressParser class, and then this function for tokenize that is called by parse

JavaScript

We can see here that tokenize calls replace, and so that is likely where your error is coming from. tokenize is probably expecting a str here (not a float), and that s.replace() is almost certainly for a string replacement.

So, your column likely has floats in it when it expects strings. The tokenize function should probably handle that better, but now it is up to you.

You should be able to resolve this by forcing your Address column to be strings (pandas will call it ‘object’).

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement