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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With