2014-12-02 54 views
1

我有一個數據幀(DF),其看起來像這樣切片大熊貓數據幀表示以外的所有列中提供

category | amount | freq 
green   10  1 
blue   5  2 
orange  7  3 
purple  5  4 

我想選擇只有「頻率」和「量」的列,並且所有行除了紫色的

我知道我可以使用df.ix選擇這樣

df.ix[['green','blue','orange'],['freq','amount']] 

列。然而,你怎麼弄的唯一值的類別欄,並選擇列whic h不是紫色的?

df.set_index(['category']) 

更新

見羅馬PEKAR對過濾掉你不想行的解決方案。

對於多行創建系列或列表(即account_group)並像這樣引用它。

names = sorted_data[sorted_data.account.isin(account_group)] 

完成這樣是一個數據幀。

然而,這是相似但語法不正確,這將返回一系列。

names = sorted_data['account'].isin(account_group) 

回答

2
>>> df 
    category amount freq 
0 green  10  1 
1  blue  5  2 
2 orange  7  3 
3 purple  5  4 

>>> df[df['category'] != 'purple'][['amount','freq']] 
    amount freq 
0  10  1 
1  5  2 
2  7  3 

更新不知道如果我理解正確的OP,但他想這樣做,也是by subtracting lists: the first list is all the rows in the dataframe, the second is purple, and the third would be list-one minus list-two which would be green, blue, orange。因此,另一種解決方案:

>>> l1 
['green', 'blue', 'orange', 'purple'] 
>>> l2 
['purple'] 
>>> l3 = [x for x in l1 if x not in l2] 
>>> l3 
['green', 'blue', 'orange'] 
>>> df[df['category'].isin(l3)][['amount','freq']] 
    amount freq 
0  10  1 
1  5  2 
2  7  3 
+0

感謝羅馬PEKAR,難道還可以通過減去名單進行:第一個列表在數據幀中的所有行,第二個是紫色的,第三個是列表一減列表 - 兩個是綠色,藍色,橙色?這可能嗎? – yoshiserry 2014-12-02 22:26:45

+0

@yoshiserry更新 – 2014-12-02 22:35:27

+0

非常棒!看到兩種解決方案! – yoshiserry 2014-12-02 22:37:35