2017-01-28 48 views
4

我有這樣從數據幀熊貓蟒刪除一個例子

 Phrase       Sentiment 

    [ good , movie ]     positive 

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

     []        negative 
     []        pOSTIVE 

列短語類型數據幀是對象和需要刪除含有[] 行和我不知道浩不使用Python它

這樣的:

Phrase       Sentiment 

    [ good , movie ]     positive 

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

@MYGz沒錯,一點 –

回答

5

您可以通過str.len()==0通過執行檢查空列表的存在和過濾在此基礎上的DF操作。

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

enter image description here

要知道空列表是存在的行:

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

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

櫃面有空字符串存在,它們的長度也將等同於零。在這種情況下,通過使用map上的自定義功能,可以根據其類型進行過濾。

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

如果他們列出的字符串表示,那麼你可以直接對他們進行過濾,以獲得子集DF

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

這似乎並不有效的名單給我。可能是OP忘了放引號或它是一個字符串。更新字符串。 – MYGz

3

空列表[]評估爲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'])