循环遍历DF列以删除具有西班牙语tex的行

2024-05-20 00:01:38 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我用来过滤所有西班牙语文本的代码:

from langdetect import detect  #detects what language is written
from  tqdm import tqdm #timing package

# 'summary_processed' is a list of sentence strings that had general text preprocessing done (lemmetization, regex removal, lowercasing, etc)
summary_processed_en = [i for i in tqdm(summary_processed) if detect(i) == 'en']

现在,这不是一个典型的条件语句,所以我不能使用常规的df[df == "X"]格式。在

我不太清楚该怎么做。任何帮助都将不胜感激。在


Tags: 代码from文本importdfissummarywhat
1条回答
网友
1楼 · 发布于 2024-05-20 00:01:38

你可以使用apply和lambda很容易地做到这一点。在

index = df['a'].apply(lambda x: detect(x) == 'en')

然后,您可以将索引应用于您想要的任何列。或者你也可以

^{pr2}$

在同一列上做。在

相关问题 更多 >