Reputation: 11
I'm trying to count the number of words that have the string: "hanger" from the column "Description". So I defined a function:
def hanger_count(title):
if 'hanger' in title.lower().split():
return True
else:
return False
Which seemed to be working correctly when I tested it with a string. But when I attempted to run the function through the data column, using the function:
ecomm['Description'].apply(hangercount)
I received an error back:
AttributeError: 'float' object has no attribute 'lower'
I think the issue is that python is seeing some of the rows in the column as objects rather than strings, is there any way I can convert it?
What do you think I'm doing wrong?
Upvotes: 1
Views: 130
Reputation: 41
You appear to have mixed data types in your column, and since lower()
is only a method for strings, you are getting an error when pandas attempts to call the function on a numeric value (in this case a float).
This quick tweak might work for you:
def hanger_count(title):
if 'hanger' in str(title).lower().split():
return True
else:
return False
Upvotes: 4
Reputation: 9019
You can just do the following using str.contains()
and count()
:
df[df['Description'].str.contains('hanger', case=False, na=False)].count()
Upvotes: 3