2014-12-02 18 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