Remove example from dataframe pandas python

I have such a data frame

     Phrase                          Sentiment

   [ good , movie ]                   positive

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

      []                             negative
      []                              pOSTIVE

The type of the phrase is an object and I need to delete lines containing [] and I don’t know how to do it using python

like this:

 Phrase                          Sentiment

   [ good , movie ]                   positive

   [wooow ,is , it ,very, good  ]   positive
+4
source share
2 answers

You can check for empty lists on str.len()==0and filter DFbased on this by performing a negation operation.

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

enter image description here

Know the lines in which empty lists are present:

df.Phrase.str.len() == 0

0    False
1    False
2     True
3     True
Name: Phrase, dtype: bool

If there are blank lines, their length will also be zero. In this case, filtering by their type would be useful using a custom function on map.

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

, , DF:

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

[] False

df[df.Phrase.astype(bool)]

                       Phrase Sentiment
0               [good, movie]  positive
1  [woow, is, it, very, good]  positive

df = pd.DataFrame([
        [['good', 'movie'], 'positive'],
        [['woow', 'is', 'it', 'very', 'good'], 'positive'],
        [[], 'negative'],
        [[], 'pOSITIVE']
    ], columns=['Phrase', 'Sentiment'])
+3

Source: https://habr.com/ru/post/1668110/


All Articles