Pandas组过滤器问题

2024-09-27 00:20:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我一辈子都搞不懂为什么filter方法拒绝对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.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: []

注意filter如何拒绝在dff3上工作。在

感谢任何帮助。在


Tags: lambdaincorehellopandasobjectfilterout
1条回答
网友
1楼 · 发布于 2024-09-27 00:20:15

如果按列名分组,则将其移动到索引,这样数据帧将变为空;如果没有其他列,请参阅:

>>> 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: []

可以按列值分组:

^{pr2}$

相关问题 更多 >

    热门问题