【发布时间】:2014-04-23 20:56:06
【问题描述】:
我刚刚从以前的版本更新到 Pandas 0.13.1 - 很高兴,这为我提供了一些选择。不幸的是,它似乎对我的一些数据整理代码造成了问题。除了从 0.11.0 更新 Pandas 版本之外,我没有进行任何更改
之前有效,现在无效的代码如下:
g_pres = g_pres.groupby(['follow','Focal','std_epoch']).dropna(0)
和/或
g_pres = g_pres.groupby(['follow','Focal','std_epoch']).drop_duplicates(0)
使用任何一个都会导致以下属性错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-169-5d3c7458da40> in <module>()
----> 1 g_pres = g_pres.groupby(['follow','Focal','std_epoch']).dropna(0)
//anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in __getattr__(self, attr)
293
294 if hasattr(self.obj, attr) and attr != '_cache':
--> 295 return self._make_wrapper(attr)
296
297 raise AttributeError("%r object has no attribute %r" %
//anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in _make_wrapper(self, name)
308 "using the 'apply' method".format(kind, name,
309 type(self).__name__))
--> 310 raise AttributeError(msg)
311
312 f = getattr(self.obj, name)
AttributeError: Cannot access callable attribute 'dropna' of 'DataFrameGroupBy' objects, try using the 'apply' method
我查看了发行说明,搜索了 groupby、drop_duplicates 和 drop_na,但我找不到任何东西来表明(至少对我而言)可能导致此更改的原因。我是初学者,所以也许我忽略了一些东西。
drop_duplicates 函数是否不再适用于 groupby 数据帧?有新的语法吗? ...这是功能还是错误?
我认为添加 inplace 方法可能意味着我需要指定以前默认的东西,但是查看相关方法的文档并没有取得任何进展。
[编辑以添加示例数据]
示例输入:
follow std_epoch Focal 0
0 1 1 53704 51602
1 1 1 53704 51602
2 1 2 53704 51602
3 2 1 53505 51509
4 2 2 53505 51509
示例输出,我想按 follow、std_epoch 和 Focal 进行分组 - 并从“0”列中按组删除重复值(在本例中,即输入中的第 2 行)。
follow std_epoch Focal 0
0 1 1 53704 51602
1 1 2 53704 51602
2 2 1 53505 51509
3 2 2 53505 51509
【问题讨论】:
-
哎呀-感谢编辑,@EdChum
-
你不能dropna到一个组;没有意义,这只是组列表;你想过滤吗?你想做什么?举个输入输出的小例子
-
gr8!根据定义,如果您在重复列上
groupby,则生成的组没有重复! -
哈哈....你是对的!具有讽刺意味的是,我们刚刚就加强 groupby 的文档进行了完整的讨论:github.com/pydata/pandas/issues/6944
-
@M.A.Kline 在 0.13 (
Begin removing methods that don’t make sense on GroupBy objects (GH4887)) 的发行说明中有一条关于此的小行,但我认为您没有找到那个是正常的... :-) .因此,以前这样做的原因是只有所有可用于数据帧的方法,也可用于 GroupBy 对象,而现在只有在白名单中明确列出的方法(有意义的方法)