从dataframe pandas python中删除一个例子

Ama*_*ghi 4 python delete-row dataframe pandas drop-down-menu

我有这样的数据帧

     Phrase                          Sentiment

   [ good , movie ]                   positive

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

      []                             negative
      []                              pOSTIVE
Run Code Online (Sandbox Code Playgroud)

列短语类型是对象,需要删除包含[]的行,我不知道如何使用python

像这样:

 Phrase                          Sentiment

   [ good , movie ]                   positive

   [wooow ,is , it ,very, good  ]   positive
Run Code Online (Sandbox Code Playgroud)

Nic*_*eli 5

您可以通过执行否定操作来检查是否存在空列表str.len()==0DF根据此进行过滤.

df[df.Phrase.str.len() != 0]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要知道存在空列表的行:

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

0    False
1    False
2     True
3     True
Name: Phrase, dtype: bool
Run Code Online (Sandbox Code Playgroud)

如果存在空字符串,它们的长度也等于零.在这种情况下,通过使用自定义函数,基于其类型进行过滤将是有帮助的map.

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

如果它们是列表的字符串表示,那么您可以直接过滤它们以获得子集DF:

df[df.Phrase != "[]"]
Run Code Online (Sandbox Code Playgroud)