Python 熊猫群过滤器问题
我一辈子都搞不明白为什么过滤方法拒绝在我的熊猫数据帧上工作 下面是一个显示我的问题的示例:Python 熊猫群过滤器问题,python,pandas,Python,Pandas,我一辈子都搞不明白为什么过滤方法拒绝在我的熊猫数据帧上工作 下面是一个显示我的问题的示例: 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.group
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: []
[99]中的:dff4
出[99]:
In[100]:dff3
出[100]:
In[101]:dff3.groups
出[101]:
{'iphone':[8537385374],
“远程api已创建”:[85363,
85364,
85365,
85412]}
在[102]:dff4.groups中
Out[102]:{'bye':[3],'bye-bye':[4],'hello':[0,1,2]}
[103]:dff4.滤波器(λx:len(x)>2)
出[103]:
A B
0你好
你好
你好
In[104]:dff3.滤波器(λx:len(x)>2)
Out[104]:
空数据帧
专栏:[来源]
索引:[]
请注意过滤器如何拒绝在dff3上工作
非常感谢您的帮助。如果您按列名分组,请将其移动到索引,这样您的数据框将变为空,如果没有其他列,请参阅:
>>> 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
我花了好几个小时想弄明白。非常感谢。