首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用字典查找/替换标记熊猫系列中的确切术语。

使用字典查找/替换标记熊猫系列中的确切术语。
EN

Stack Overflow用户
提问于 2022-02-08 17:18:31
回答 2查看 225关注 0票数 1

使用字典,我需要找到并替换熊猫系列中的术语,根据以下标准:

如果在熊猫系列中找到字典键(例如,在“母版”:“硕士博士”中,替换结果将是“硕士”occurs)

  • maintain记录完整性(即,由于需要保存完整的唯一记录而不能使用一袋单词),则
  1. 字典值将替换它们。
  2. 只应替换精确的匹配项(例如,如果关键字:value为“rf”:“随机森林”,替换时不应将“性能”转换为“perandom forestormance”);显然,这是regex=True造成的)

数据: term_fixes是字典,df‘’job_description‘是所感兴趣的标记系列

代码语言:javascript
复制
term_fixes = {'rf': 'random forest',
              'mastersphd': 'masters phd',
              'curiosity': 'curious',
              'trustworthy': 'ethical',
              'realise': 'realize'}


df = pd.DataFrame(data={'job_description': [['knowledge', 'of', 'algorithm', 'like', 'rf'],
                                            ['must', 'have', 'a', 'mastersphd'],
                                            ['trustworthy', 'and', 'possess', 'curiosity'],
                                            ['we', 'realise', 'performance', 'is', 'key']]})

**注:我也尝试过未标记化的数据结构(失败),但更喜欢标记化,因为我有更多的NLP要做。

代码语言:javascript
复制
df = pd.DataFrame(data={'job_description': ['knowledge of algorithm like rf',
                                            'must have a mastersphd',
                                            'must be trustworthy and possess curiosity',
                                            'we realise performance is critical']})

**期望的结果(请注意,性能中的“rf”没有被“随机森林”所取代):df‘作业_描述’

代码语言:javascript
复制
0    ['knowledge' 'of' 'algorithm' 'like' 'random' 'forest']
1                        ['must' 'have' 'a' 'masters' 'phd']
2          ['must' 'be' 'ethical' 'and' 'possess' 'curious']
3             ['we' 'realize' 'performance' 'is' 'critical']

我尝试过许多方法。失败:df['job_description'].replace(list(term_fixes.keys()), list(term_fixes.values()), regex=False, inplace=True)

失败:df['job_description'].replace(dict(zip(list(term_fixes.keys()), list(term_fixes.values()))), regex=False, inplace=True)

失败:df['job_description'] = df['job_description'].str.replace(term_fixes, regex=False)

失败:df['job_description'] = df['job_description'].str.replace(str(term_fixes.keys()), str(term_fixes.values()), regex=True)

我来得最近的是

代码语言:javascript
复制
df['job_description'] = df_jobs['job_description'].replace(term_fixes, regex=True)

但是,任何匹配的regex=True标志(比如上面的'rf‘和'performance’示例)。不幸的是,将标志更改为regex=False无法替换任何东西。我在文档中寻找另一个我可以使用的论点,但没有运气。注意,这使用了未标记化的结构。

任何帮助都将不胜感激。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-08 22:54:08

使用df的“令牌化”版本。

代码语言:javascript
复制
df['job_description'] = df['job_description'].explode().replace(term_fixes).groupby(level=-1).agg(list)

# explode to get single terms per "cell"
# replace to replace the terms in "term_fixes"
# groupby to reverse the previous explode and return to a column of lists

                                   job_description
0  [knowledge, of, algorithm, like, random forest]
1                     [must, have, a, masters phd]
2                 [ethical, and, possess, curious]
3              [we, realize, performance, is, key]

如果需要在空格上拆分新的术语,则可以在最终的.str.split().explode()之前添加另一个中间步骤groupby

代码语言:javascript
复制
df['job_description'] = df['job_description'].explode().replace(term_fixes).str.split().explode().groupby(level=-1).agg(list)

                                    job_description
0  [knowledge, of, algorithm, like, random, forest] # random forest is now split
1                     [must, have, a, masters, phd] # masters phd is now split
2                  [ethical, and, possess, curious]
3               [we, realize, performance, is, key]
票数 2
EN

Stack Overflow用户

发布于 2022-02-08 17:30:37

您可以使用下面这样的方法来处理未标记的数据。

代码语言:javascript
复制
for k in term_fixes:
    df['job_description'] = (df['job_description'].str.replace(r'(^|(?<= )){}((?= )|$)'.format(k), term_fixes[k]))

print(df)
                             job_description
0  knowledge of algorithm like random forest
1                    must have a masters phd
2        must be ethical and possess curious
3         we realize performance is critical
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71038063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档