Search code examples
pythonpandasnlpdrop

Return rows with some text in them and delete the rest


I have a dataframe df with values as below:

Common_words         count
0   realdonaldtrump  2932
2   new              2347
3                    2030
4   trump            2013
5   good             1553
6                    1440
7   great            200

I only need the rows where there is certain text. For e.g rows which have blank value like row 3 and row6 need to be removed.

Tried:

df = df.dropna(how='any',axis=0) 

but still i get the same result. I feel these are not null values but spaces, so I also tried below:

df.Common_words = df.Common_words.str.replace(' ', '')

But still same result. Row 3 and 6 are still not removed. What to do?


Solution

  • You can do:

    df.Common_words =  df.Common_words.replace(r"\s+", np.NaN, regex=True)
    
    df.dropna()