Mark Ginsburg
Mark Ginsburg

Reputation: 2269

mysterious Python Pandas lambda function error

I have a pandas dataframe and I have a column called 'email'. I have verified the dtype is object. It contains normally formatted emails such as [email protected]

When I do this:

$ df['emaillower'] = df['email'].apply(lambda x: x.lower())

I get this:

Traceback (most recent call last):

File "<ipython-input-153-e951d53133eb>", line 1, in <module>
df['emaillower'] = df['email'].apply(lambda x: x.upper())

File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\series.py", 
line 
2355, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)

File "pandas\_libs\src\inference.pyx", line 1569, in 
pandas._libs.lib.map_infer (pandas\_libs\lib.c:66440)

File "<ipython-input-153-e951d53133eb>", line 1, in <lambda>
df['emaillower'] = df['email'].apply(lambda x: x.upper())

AttributeError: 'float' object has no attribute 'upper'      

What is going on?

Upvotes: 2

Views: 1579

Answers (2)

Espoir Murhabazi
Espoir Murhabazi

Reputation: 6376

Will suggest using str function from pandas

df['emaillower'] = df['email'].astype(np.str).str.upper()

I have used astye(np.str) to be sure all values are converted to string .

Upvotes: 2

Sevy
Sevy

Reputation: 698

One of the entries in the column 'email' is a float, not a string, and it doesn't know how to do upper() on a float. This is common when one entry is empty and is converted to NaN - this is read as a float and that's the source of your error. Something like this may fix the problem:

df['emaillower'] = df['email'].apply(lambda x: x.upper() if type(x) is str else 'empty')

Also want to note that you call the column emaillower but you are actually making it upper case - this might cause some confusion in the future

Upvotes: 3

Related Questions