2013-12-16 82 views
0

我不能为我的生活弄清楚为什么过滤器方法拒绝在熊猫的数据框中工作。熊猫群组过滤器问题

这里是在我的问题的例子:

In [99]: dff4 
Out[99]: <pandas.core.groupby.DataFrameGroupBy object at 0x1143cbf90> 

In [100]: dff3 
Out[100]: <pandas.core.groupby.DataFrameGroupBy object at 0x11439a810> 

In [101]: dff3.groups 
Out[101]: 
{'iphone': [85373, 85374], 
'remote_api_created': [85363, 
    85364, 
    85365, 
    85412]} 

In [102]: dff4.groups 
Out[102]: {'bye': [3], 'bye bye': [4], 'hello': [0, 1, 2]} 

In [103]: dff4.filter(lambda x: len(x) >2) 
Out[103]: 
    A  B 
0 0 hello 
1 1 hello 
2 2 hello 

In [104]: dff3.filter(lambda x: len(x) >2) 
Out[104]: 
Empty DataFrame 
Columns: [source] 
Index: [] 

通知过滤器如何拒绝对DFF3工作。

任何帮助表示赞赏。

回答

2

如果通过列名小组,你将它移动到索引,让您的数据框为空,如果没有其他的列存在,请参阅:

>>> def report(x): 
...  print x 
...  return True 
>>> df 
        source 
85363 remote_api_created 
85364 remote_api_created 
85365 remote_api_created 
85373    iphone 
85374    iphone 
85412 remote_api_created 

>>> df.groupby('source').filter(report) 
Series([], dtype: float64) 
Empty DataFrame 
Columns: [] 
Index: [85373, 85374] 
Series([], dtype: float64) 
Empty DataFrame 
Columns: [source] 
Index: [] 

您可以通过列值组:

>>> df.groupby(df['source']).filter(lambda x: len(x)>2) 
        source 
85363 remote_api_created 
85364 remote_api_created 
85365 remote_api_created 
85412 remote_api_created 
+0

我花了数小时试图弄清楚这一点。万分感谢。 – Dalupus