Remove the rows from pandas dataframe, that has sentences longer than certain word length
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
You can count the spaces:
df[df.Y.str.count('\s+').lt(3)]
X Y
1 1 An apple
2 2 glass of water