2013-12-16 80 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