Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列中删除短/长字符串行_Python_Pandas_String - Fatal编程技术网

Python 从列中删除短/长字符串行

Python 从列中删除短/长字符串行,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个单词,最大值

我有一个数据框,其中有一列字符串和一列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个单词,最大值设置为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”]