Use word count in Pandas dataframe to drop rows with only one word

Viewed 4047

I have a dataframe (data) with 2 records:

id    text
0001  The farmer plants grain
0002  tuna

I want to count the number of words in the text column of this dataframe and drop rows with only one word.

I know how to count the number of words:

count = data['text'].str.split().str.len()

How do I use the results to run an IF statement that will drop rows in the dataframe? Any IF statements such as...

if count == 1:
    print('drop')

...results in this error:

Traceback (most recent call last):

  File "<ipython-input-118-b3fcb0218e8e>", line 32, in <module>
    if count == 1:

  File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\generic.py", line 917, in __nonzero__
    .format(self.__class__.__name__))

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I have read the Pandas documentation and other SO questions around this error, but I can't seem to get the solutions to apply correctly to my issue with the IF statement.

Any advise is greatly appreciated! As I am relatively new to SO, please let me know if there's anything I can do to improve my question.

4 Answers

Probably late to answer but it could help new viewers.
You can easly find the indexes of the rows, that match what you want and drop them from the dataframe.

wantedRows = data[data['text'].str.split().str.len()==1].index 
data =  data.drop(wantedRows, axis = 0)
Related