[英]Pandas 0.13.1 use of groupby( ) with drop_duplicates( ) or dropna ( )
我剛剛從以前的版本更新到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
在示例輸出中,我要按以下,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
解決方案:基本上,重新考慮問題。 如我的評論中所述,我不需要使用groupby刪除重復項,只需將它們放在之前代碼中的同一行中。 我仍然不清楚為什么這會導致以前沒有的錯誤(也許應該總是拋出錯誤,盡管確實會產生我想要的結果!)。 但是,我現在僅用兩行代碼來完成此操作。
刪除重復項:
df = df.drop_duplicates()
要創建分組對象:
df = df.groupby(['column1','column2'])
為了在分組的DataFrame上應用方法; 您需要使用如下循環:
display = IPython.display.display
g_pres = g_pres.groupby(['follow','Focal','std_epoch'])
for a,b in g_pres:
z = b.dropna(0)
display(z)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.