Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何用字典值替换pd.Series列表值_Python 3.x_Pandas_Dictionary_Replace_Series - Fatal编程技术网

Python 3.x 如何用字典值替换pd.Series列表值

Python 3.x 如何用字典值替换pd.Series列表值,python-3.x,pandas,dictionary,replace,series,Python 3.x,Pandas,Dictionary,Replace,Series,使用NLTK对单词进行词干分析后,得到字典: > {'golden': 'gold', 'wonderfully': 'wonder', 'damaging': 'damag', > 'useless': 'use', 'toys': 'toy', 'ducks': 'duck'} 0 ['want', 'buy', 'toy', 'duck'] 1 ['street']

使用NLTK对单词进行词干分析后,得到字典:

> {'golden': 'gold',  'wonderfully': 'wonder',  'damaging': 'damag', 
> 'useless': 'use',  'toys': 'toy',  'ducks': 'duck'}
0                ['want', 'buy', 'toy', 'duck']
1                                    ['street']
2          ['damag', 'toy', 'isolating', 'toy']
3                           ['use', 'clothing']
因此,我希望替换pd.Series中的现有单词,其中值为列表:

0              ['want', 'buy', 'toys', 'ducks']
1                                    ['street']
2     ['damaging', 'toys', 'isolating', 'toys']
3                       ['useless', 'clothing']
我想作为玩具的替代玩具等输出。。正如字典中所说:

> {'golden': 'gold',  'wonderfully': 'wonder',  'damaging': 'damag', 
> 'useless': 'use',  'toys': 'toy',  'ducks': 'duck'}
0                ['want', 'buy', 'toy', 'duck']
1                                    ['street']
2          ['damag', 'toy', 'isolating', 'toy']
3                           ['use', 'clothing']
试着爆炸,然后替换,然后返回,d是你的指令

out = s.explode().replace(d).groupby(level=0).agg(list)
您可以使用:

输出


这一解决办法确实有效。只需使用d来定义查找字典,可能还不清楚。