我有一个pandasdf
,它有6列,最后一列是input_text
。我想从df
中删除该列中包含非英语文本的所有行。我想使用langdetect
的detect
函数
一些模板
from langdetect import detect
import pandas as pd
def filter_nonenglish(df):
new_df = None # Do some magical operations here to create the filtered df
return new_df
df = pd.read_csv('somecsv.csv')
df_new = filter_nonenglish(df)
print('New df is: ', df_new)
注意!其他5列是什么并不重要。
另请注意:使用detect
非常简单:
t = 'I am very cool!'
print(detect(t))
输出为:
en
您可以在
df
上执行以下操作,并在input_text
列中获取所有包含英文文本的行:因此,基本上只需将
langdetect.detect
函数应用于input_text
列中的值,并获取所有那些文本被检测为“en”的行相关问题 更多 >
编程相关推荐