Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the rows from pandas dataframe, that has sentences longer than certain word length

I want to remove the rows from the pandas dataframe, that contains the strings from a particular column whose length is greater than the desired length.

For example:

Input frame:

X    Y
0    Hi how are you.
1    An apple
2    glass of water
3    I like to watch movie

Now, say I want to remove the rows which has the string of words with length greater than or equal to 4 from the dataframe.

The desired output frame must be:

X    Y
1    An apple
2    glass of water

Row with value 0,3 in column 'X' is removed as the number of words in column 0 is 4 and column 3 is 5 respectively.

like image 660
Ashwin Geet D'Sa Avatar asked Jun 12 '19 13:06

Ashwin Geet D'Sa


2 Answers

First split values by whitespace, get number of rows by Series.str.len and check by inverted condition >= to < with Series.lt for boolean indexing:

df = df[df['Y'].str.split().str.len().lt(4)]
#alternative with inverted mask by ~
#df = df[~df['Y'].str.split().str.len().ge(4)]
print (df)
   X               Y
1  1        An apple
2  2  glass of water
like image 154
jezrael Avatar answered Oct 04 '22 00:10

jezrael


You can count the spaces:

df[df.Y.str.count('\s+').lt(3)]

   X               Y
1  1        An apple
2  2  glass of water
like image 38
piRSquared Avatar answered Oct 04 '22 00:10

piRSquared