使用lambda的字典中特定键的值?

时间:2017-10-08 20:55:50

标签: python dictionary lambda ipython-notebook graphlab

我的产品数组如下表所示:

+---------------------------+--------------------------------+--------------------------------+
|    name                   |  review                        | word_count                     |
+---------------------------+--------------------------------+--------------------------------+
|                           |                                | {'and': 5, 'wipes': 1,         |
| Planetwise                |  These flannel wipes are OK,   | 'stink': 1, 'because' : 2, ... |
| Flannel Wipes             |  but in my opinion ...         |                                |
|                           |                                |                                |
+---------------------------+--------------------------------+--------------------------------+
|                           |                                | {'and': 3, 'love': 1,          |
| Planetwise                |  it came early and was not     | 'it': 2, 'highly': 1, ...      |
| Wipes Pouch               |  disappointed. i love ...      |                                |
|                           |                                |                                |
+---------------------------+--------------------------------+--------------------------------+
|                           |                                | {'shop': 1, 'noble': 1,        |
|                           |                                | 'is': 1, 'it': 1, 'as': ...    |
| A Tale of Baby's Days     |  Lovely book, it's bound       |                                |
|  with Peter Rabbit ...    |  tightly so you may no ...     |                                |
|                           |                                |                                |
+---------------------------+--------------------------------+--------------------------------+

基本上,word_count列包含dictionary(key : value)review列句子的单词出现。

现在我想构建一个新的列名and,如果and作为{{1}中的关键字存在word_count,则and字典中应包含值word_count }}列,然后是值,如果它不作为键存在,那么0

对于前3行,新的and列看起来像这样:

+------------+
|    and     |
+------------+
|            |
| 5          |
|            |
|            |
+------------+
|            |
| 3          |
|            |
|            |
+------------+
|            |
| 0          |
|            |
|            |
+------------+

我写了这段代码并且它正常工作:

def wordcount(x):
    if 'and' in x:
        return x['and']
    else:
        return 0

products['and'] = products['word_count'].apply(wordcount);

我的问题:我有什么方法可以使用lambda来做到这一点吗?

到目前为止,我所做的是:

products['and'] = products['word_count'].apply(lambda x : 'and' in x.keys());

这会在列中仅返回01。我可以在上面的行中添加哪些内容,以便当products['and']中的密钥作为密钥存在时,and包含密钥products['word_count']的值?

我正在使用ipython notebook和graphlab。

2 个答案:

答案 0 :(得分:2)

你有正确的想法。只需返回x['and']的值(如果存在),否则返回0

例如:

data = {"word_count":[{"foo":1, "and":5}, 
                      {"foo":1}]}
df = pd.DataFrame(data)
df.word_count.apply(lambda x: x['and'] if 'and' in x.keys() else 0)

输出:

0    5
1    0
Name: word_count, dtype: int64

答案 1 :(得分:2)

我不确定products['word_count'].apply(wordcount)是做什么的,但是从你的问题的其余部分来看,当你可以做一些像lambda一样的事情:< / p>

products['and'] = (
    lambda p: p['and']['and'] if 'and' in p['and'] else 0)(products)

它有点丑陋和笨拙,所以我建议使用内置字典get()方法,因为它已经过调试,更短,更易于维护,以及更快:

products['and'] = products['and'].get('and', 0)

你使用lambda时的注意力让我想起了有些人称之为Law of the Instrument的内容:&#34; ...如果你拥有的唯一工具是锤子,那就很诱人了好像它是一个钉子&#34;。