问题描述
用NLTK词干后,得到字典:
> {'golden': 'gold','wonderfully': 'wonder','damaging': 'damag',> 'useless': 'use','toys': 'toy','ducks': 'duck'}
所以我想替换 pd.Series 中的现有单词(其中值是列表):
0 ['want','buy','toys','ducks']
1 ['street']
2 ['damaging','isolating','toys']
3 ['useless','clothing']
我希望输出为(replace玩具为玩具等......如字典中):
0 ['want','toy','duck']
1 ['street']
2 ['damag','toy']
3 ['use','clothing']
解决方法
尝试 explode
然后 replace
和 agg
回来,d
是你的字典
out = s.explode().replace(d).groupby(level=0).agg(list)
,
您可以使用 apply:
import pandas as pd
# the replacement dictionary
replacements = {'golden': 'gold','wonderfully': 'wonder','damaging': 'damag','useless': 'use','toys': 'toy','ducks': 'duck'}
# setup of the Series
s = pd.Series([['want','buy','toys','ducks'],['street'],['damaging','isolating','toys'],['useless','clothing']])
def replace(lst,repl):
"""This function receives a list and replaces the elements with the values of the keys in repl"""
return [repl.get(e,e) for e in lst]
out = s.apply(replace,args=(replacements,))
print(out)
输出
0 [want,buy,toy,duck]
1 [street]
2 [damag,isolating,toy]
3 [use,clothing]
dtype: object