2017-07-17 121 views
0

我仍然喜歡使用python和熊貓。我正在努力改進關鍵字評估。我的DF看起來像這樣爲什麼這個條件lambda函數不會返回預期的結果?

Name Description 
Dog Dogs are in the house 
Cat Cats are in the shed 
Cat Categories of cats are concatenated 

I am using a keyword list like this ['house', 'shed', 'in'] 

我的lambda函數看起來像這樣

keyword_agg = lambda x: ' ,'.join x if x is not 'skip me' else None 

我使用的功能來識別和得分每行關鍵字匹配

def foo (df, words): 
    col_list = [] 
    key_list= [] 
    for w in words: 
     pattern = w 
     df[w] = np.where(df.Description.str.contains(pattern), 1, 0) 
     df[w +'keyword'] = np.where(df.Description.str.contains(pattern), w, 
          'skip me') 
     col_list.append(w) 
     key_list.append(w + 'keyword') 
    df['score'] = df[col_list].sum(axis=1) 
    df['keywords'] = df[key_list].apply(keyword_agg, axis=1) 

功能追加關鍵字添加到使用工作的列中,然後根據匹配創建1或0。該函數還會創建一個包含「詞+關鍵字」的列,併爲每行創建單詞或「跳過我」。

我期待的應用這樣

df['keywords'] = df[key_list].apply(keyword_agg, axis=1) 

返回

Keywords 
in, house 
in, shed 
None 

到工作,而不是我得到

Keywords 
in, 'skip me' , house 
in, 'skip me', shed 
'skip me', 'skip me' , 'skip me' 

有人可以幫我解釋一下,爲什麼 '跳過我'字符串正在顯示,當我試圖排除他們?

+2

'是not'的身份。你想要'x!=「skip me」'請參閱[爲什麼在Python中使用'=='或'is'來比較字符串有時會產生不同的結果?](https://stackoverflow.com/questions/1504717/why -does-comparison-strings-in-python-using-or-is-sometimes-produce) – TemporalWolf

+0

首先,你爲什麼要使用'lambda'?你將它分配給一個名稱,從而消除了'lambda'具有*的唯一優點:它是匿名的。其次,我很肯定'keyword_agg = lambda x:','。如果x不是'skip me',則加入x。否則None'是SyntaxError。 –

回答

6

is運營商(和is not)檢查引用等於

你應該使用等號這將爲大多數原語的檢查值相等

lambda x: ' ,'.join(x) if x != 'skip me' else None
+0

很確定這是一個SyntaxError。 –

+0

@ juanpa.arrivillaga和MSeifert:謝謝。修復。 –

+0

@ MSeifert其實,我不認爲這是你想要的。我認爲他們實際上想要'','。加入(如果s!='skip me',s代表s)' –

相關問題