2017-09-29 146 views
0

我需要你的燈! 我有這個代碼數據幀的構建:熊貓數據框過濾對象列?

import itertools as ite 
import pandas as pd 

items=[1,2,3,4,5,6,7,8,9,10,11,12] 
data = [200,250,190,0,132,149,168,0,198,184,176,203] 

result=[(combi,len(da),sum(da),min(da),max(da)) 
     for nbCombi in range(5,10) 
       for combi,da in zip(ite.combinations(items, nbCombi),ite.combinations(data, nbCombi))] 

df=pd.DataFrame(result,columns=["Combinaison","Nb","Poids","Min","Max"]) 
df['Ecart']=df['Min']-df['Max'] 

這給我十萬的結果,但這些節點可能是例如一個:

Combinaison    Nb  Poids  Min  Max Ecart 
(1, 2, 5, 6, 10, 11)  6  942  0  250  250 

的combinaison列的dtypes爲對象。它是一個數組嗎?

如何過濾結果以排除與項目1或5找到的組合? 我知道如何過濾整數列上的數據幀,但我不知道如何處理類型是數組或對象。 非常感謝。

回答

1

我會使用過濾使用適用於:

df[df.Combinaison.apply(lambda val: not any([i in val for i in [1,5]]))] 

編輯:在一般情況下,它可能是更好地把元組的每個值成一列。儘管如此,我還是假設你將它作爲一個元組保留下來。

編輯2:我原來的解決方案使用了一個額外的lambda,這是不必要的。

1

可以使用使用與np.in1d適用於檢查是否兩個或任一個元件在Combinaison柱被預設即

import numpy as np 
df[~df.Combinaison.apply(lambda x : any(np.in1d([1,5],np.array(x))))] 

輸出:

 
      Combinaison Nb Poids Min Max Ecart 
337 (2, 3, 4, 6, 7) 5 757 0 250 -250 
338 (2, 3, 4, 6, 8) 5 589 0 250 -250 
339 (2, 3, 4, 6, 9) 5 787 0 250 -250 
340 (2, 3, 4, 6, 10) 5 773 0 250 -250 
341 (2, 3, 4, 6, 11) 5 765 0 250 -250 

要與兩個過濾Combinaison 1,5目前使用all而不是any