如果值包含单词的复数形式,如何删除一行? [英] How to delete a row if value include plural form of the words?

查看:103
本文介绍了如果值包含单词的复数形式,如何删除一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果值包含单词的复数形式,并且仅存在单词的单形式,则删除一行,例如DataFrame包含'test'和'tests'或'company'和'companys',即删除测试"和公司".接下来我想应用另一个规则构词规则

Delete a row if value include the plural form of the words and only if exists the single form of word too, for example DataFrame contains 'test' and 'tests' or 'company' and 'companies', that is will be delete 'tests' and 'companies'. And next I want to apply another rules word formation rules

原始数据框

d0 = [{'word':'test', 'count':22}, {'word':'tests', 'count':11},{'word':'company', 'count':2},{'word':'companies', 'count':5}]
df0 = pd.DataFrame(d0)

理想的数据帧

d1 = [{'word':'test', 'count':22}, {'word':'company', 'count':11}]
df1 = pd.DataFrame(d1)

推荐答案

怎么样:

import nltk 
nltk.download('wordnet')

from nltk.stem import WordNetLemmatizer

wnl = WordNetLemmatizer()

d0 = [{'word':'test', 'count':22}, {'word':'tests', 'count':11},{'word':'company', 'count':2},{'word':'companies', 'count':5},
     {'word':'debris', 'count':5}]

def not_plural(word):
    lemma = wnl.lemmatize(word, 'n')
    not_plural = False if word is not lemma else True
    return not_plural

df1 = df0[df0.word.apply(not_plural)]
df1

      word  count
0     test     22
2  company      2
4   debris      5

信用:如何在python中测试一个单词是否是单数形式?

这篇关于如果值包含单词的复数形式,如何删除一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆