Python 从列中删除短/长字符串行
我有一个数据框,其中有一列字符串和一列IDPython 从列中删除短/长字符串行,python,pandas,string,Python,Pandas,String,我有一个数据框,其中有一列字符串和一列ID >>> import pandas as pd >>> df ID Sentence 0 The cat is running away 1 The lazy dog jumped over the brown fox just now 2 Hello 我想做的是删除字符串太短或太长的行。例如,我想将字符串的最小值设置为2个单词,最大值
>>> import pandas as pd
>>> df
ID Sentence
0 The cat is running away
1 The lazy dog jumped over the brown fox just now
2 Hello
我想做的是删除字符串太短或太长的行。例如,我想将字符串的最小值设置为2个单词,最大值设置为8个单词。过滤掉阈值后,只返回ID#1
ID Sentence
0 The cat is running away
有谁能给我一个建议吗?您好这可以通过创建一个包含单词计数的新列,然后过滤您的df来实现
df["Sentence"]=df["Sentence"].str.split()
df["WordsCount"]=df["Sentence"].apply(lambda x: len(x))
df=df[(df["WordsCount"]>= 2)&(df["WordsCount"]<=8)]
df[“句子”]=df[“句子”].str.split()
df[“wordscont”]=df[“句子”].应用(lambda x:len(x))
df=df[(df[“WordScont”]>=2)和(df[“WordScont”]