简体   繁体   中英

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

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]

在此输入图像描述

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 != "[]"]

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM