Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 737
Amal Kostali Targhi Avatar asked Feb 06 '23 05:02

Amal Kostali Targhi


2 Answers

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 != "[]"]
like image 72
Nickil Maveli Avatar answered Feb 08 '23 08:02

Nickil Maveli


empty lists [] evaluate to False

df[df.Phrase.astype(bool)]

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

setup

df = pd.DataFrame([
        [['good', 'movie'], 'positive'],
        [['woow', 'is', 'it', 'very', 'good'], 'positive'],
        [[], 'negative'],
        [[], 'pOSITIVE']
    ], columns=['Phrase', 'Sentiment'])
like image 33
piRSquared Avatar answered Feb 08 '23 08:02

piRSquared