Search code examples
pythonpandasdataframedrop-down-menudelete-row

delete an example from dataframe pandas python


i have a dataframe like this

     Phrase                          Sentiment

   [ good , movie ]                   positive

   [wooow ,is , it ,very, good  ]   positive

      []                             negative
      []                              pOSTIVE

the column Phrase type is object and need to delete the rows containing [] and i don't know ho do it using python

like this:

 Phrase                          Sentiment

   [ good , movie ]                   positive

   [wooow ,is , it ,very, good  ]   positive

Solution

  • You can check the presence of empty lists by str.len()==0 and filter the DF based on this by performing a negation operation.

    df[df.Phrase.str.len() != 0]
    

    enter image description here

    To know the rows where empty lists are present:

    df.Phrase.str.len() == 0
    
    0    False
    1    False
    2     True
    3     True
    Name: Phrase, dtype: bool
    

    Incase there are empty strings present, their length would also equate to zero. In this case, filtering on the basis of their type would be helpful by using a custom function on map.

    df[df.Phrase.map(lambda x: len(x) if isinstance(x, list) else None) != 0]
    

    If they are string representation of lists, then you could filter directly on them to get the subsetted DF:

    df[df.Phrase != "[]"]