dict_trim_by_keys does not work in a lambda function

User 3142 | 1/28/2016, 7:07:07 PM

Hi, This line works

In [139]:

products['word_count'] products['wordcount'].dicttrimbykeys(["awesome"],exclude=False) Out[139]: dtype: dict Rows: 166752 [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, ... ]

But, not this one In [148]:

products['awesome_count'] = products.apply(lambda x: x['wordcount'].dicttrimbykeys(["awesome"],exclude=False)) products['awesome_count'] = products.apply(lambda x: x['wordcount'].dicttrimbykeys(["awesome"],exclude=False))


AttributeError Traceback (most recent call last) <ipython-input-148-78e8163a8f0a> in <module>() ----> 1 products['awesome_count'] = products.apply(lambda x: x['wordcount'].dicttrimbykeys(["awesome"],exclude=False))

C:\Users\ning1595\AppData\Local\Dato\Dato Launcher\lib\site-packages\graphlab\datastructures\sframe.pyc in apply(self, fn, dtype, seed) 2585 assert callable(fn), "Input must be callable" 2586 testsf = self[:10] -> 2587 dryrun = [fn(row) for row in test_sf] 2588 if dtype is None: 2589 dtype = SArray(dryrun).dtype()

<ipython-input-148-78e8163a8f0a> in <lambda>(x) ----> 1 products['awesome_count'] = products.apply(lambda x: x['wordcount'].dicttrimbykeys(["awesome"],exclude=False))

AttributeError: 'dict' object has no attribute 'dicttrimby_keys'

Does anyone know what i'm doing wrong ?

Comments

User 16 | 1/29/2016, 4:44:46 AM

Hi sathishningaiah -

The x inside of the lambda is just a python dictionary (i.e. a dict). It's not an SArray. So it does not have a dict_trim_by_keys function.

Let us know if you have any other questions.

Toby


User 3142 | 1/29/2016, 5:42:47 PM

Thanks a lot Toby. I will have to try and figure out how to do this some other way of selecting those keys which interest me.