2017-02-25 5 views
0

別の列の単語のリストに対して単語ステミング機能を実行して、新しいpandasカラムを作成したいとします。私はapplyとlambdaを使って単一の文字列をトークン化することができますが、これを単語のリストに渡って実行する場合にどのように推論するのか分かりません。リストをpandasデータフレームのリストを使って作成する

test = {'Statement' : ['congratulations on the future','call the mechanic','more text'], 'Other' : [2,3,4]} 
df = pd.DataFrame(test) 
df['tokenized'] = df.apply (lambda row: nltk.word_tokenize(row['Statement']), axis=1) 

私はループの入れ子になったとそれを解決する可能性が知っているが、それは非効率ですし、SettingWithCopyWarningでの結果:

df['stems'] = '' 
for x in range(len(df)): 
    print(len(df['tokenized'][x])) 
    df['stems'][x] = row_stems=[] 
    for y in range(len(df['tokenized'][x])): 
     print(df['tokenized'][x][y]) 
     row_stems.append(stemmer.stem(df['tokenized'][x][y])) 

がこれを行うには良い方法ではないですか?

EDIT:確かに、

Other  Statement      tokenized        stems 
0 2   congratulations on the future [congratulations, on, the, future] [congratul, on, the, futur] 
1 3   call the mechanic    [call, the, mechanic]     [call, the, mechan] 
2 4   more text      [more, text]       [more, text] 
+0

あなたは結果がどのようなものかの例で編集できますか? –

答えて

1

ループを実行する必要はありません:

はここで結果がどのように見えるかの例です。少なくとも明示的なループではありません。リストの理解はうまくいくでしょう。あなたがポーターのステマーps使うと仮定すると、

df['stems'] = df['tokenized'].apply(lambda words: 
            [ps.stem(word) for word in words]) 
関連する問題