python - 如果句子中有重复的单词如何删除行
问题描述
我有一个清单
top = ['GME', 'MVIS', 'TSLA', 'AMC']
我有一个数据集
dt ... text
0 2021-03-19 20:59:49+06 ... I only need TSLA TSLA TSLA TSLA to hit 20 eod to make up for a...
1 2021-03-19 20:59:51+06 ... Oh this isn’t good
2 2021-03-19 20:59:51+06 ... lads why is my account covered in more GME ...
3 2021-03-19 20:59:51+06 ... I'm tempted to drop my last 800 into some TSLA...
所以我想要做的是检查句子中的行中是否包含超过 3 个单词我想删除这一行
谢谢你的帮助
解决方案
让我们编写一个函数来确定在给定的句子中,列表 "top" 中是否有超过 3 个单词:
def check_words(sentence,top):
words = sentence.split()
count = 0
for word in words :
if word in top :
count+=1
return(count>3)
然后,您要创建一个 True/False 列,无论句子是否包含列表中的 3 个以上的单词。让我们使用 pandas 数据框结构:
dataframe['Contains_3+_words'] = dataframe.apply(lambda r : check_words(r.text,top), axis=1)
然后我们只保留列表中没有包含 3+ 个单词的句子的行:
dataframe = dataframe[dataframe['Contains_3+_words']==False]]
此外,您可以删除我们创建的列:
dataframe.drop(['Contains_3+_words'], axis=1, inplace=True)
推荐阅读
- android - 有没有办法清除 Firebase 回收器适配器,以便它再次加载新数据
- javascript - 带有 JavaScript 的数字螺旋矩阵...结果存在问题
- python - 在表中获取所有记录的父母/孩子
- docker - Kubernetes 内存和persistenvolumeclaims
- android - 使用 Gson 提取 json 对象值
- ios - 当应用程序在后台运行或关闭时,Flutter Firebase Messaging 在 IOS 上不起作用
- python - dict 对象在 Python 按键记录器中没有属性附加错误消息
- python - 在 -1 和 1 之间缩放向量
- oauth-2.0 - Backend & auth0 - 在本地存储访问令牌,在变量中还是获取新的?
- drools - 从 https://start.jbpm.org/ 部署应用程序后无法看到 Business Central Web 应用程序