There are a couple of things that I think will help you with this problem. The first one is converting your "awesome_count" function to a lambda function. Assuming here that the goal of awesome_count is to return the number of times the word "awesome" appears, I know that products['word_count'] contains a dictionary of words and counts (i.e. "and" => 5, "awesome" => 2, "awful = "1"). The hard work here is already done for you since you have all of the counts in products['word_count']. The only thing to be careful of is that the word you're looking for might not exist in the list.
def awesome_count():
if 'awesome' in products['word_count']:
return products['word_count']['awesome']
else
return 0L
The function here checks to see if "awesome" is in the word list. If it is, then we just return products['word_count']['awesome'] (i.e. the number of times awesome occurs). If 'awesome' does not exist, then we default to 0.
So let's turn this into a lambda. Based on his line:
products['awesome'] = products['word_count'].apply(awesome_count())
Each call to the lambda function is passing in products['word_count']. In our lambda function, this will be x.
lambda x: x['awesome'] if 'awesome' in x else 0L
This is the same as above, but in the lambda format. So combine that for:
products['awesome'] = products['word_count'].apply(lambda x: x['awesome'] if 'awesome' in x else 0L)
This will work, but we can do better. Instead of hard coding the word 'awesome', let's use something more generic:
word='awesome'
products[word] = products['word_count'].apply(lambda x: x[word] if word in x else 0L)
Now we have something significantly more generic that we can plug any word into. So let's say we have a list of words we need counts for. We can do this for everything in the list.
word_list = ['awesome','and','some','other','words']
for word in word_list:
products[word] = products['word_count'].apply(lambda x: x[word] if word in x else 0L)
This is a nice and generic solution that can be used to on any number of words. Happy coding.