如何在Pandas df/series上使用.groupy()后预览行
在使用 df.groupby(df.index.month)
之后,我想查看我的 DataFrame
,但可惜的是,.head
会把分组格式去掉,而 df['col'][:3]
则返回了以下错误:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-154-6783abceafb8> in <module>()
1 test= sve_DOC
2 test = test.groupby(test.index.month)
----> 3 print test['DOC_mg/L'][:3]
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc in __getitem__(self, key)
487
488 def __getitem__(self, key):
--> 489 raise NotImplementedError
490
491 def _make_wrapper(self, name):
NotImplementedError:
有没有什么办法解决这个问题呢?
更新:在检查完分组后,我想对数据进行一些操作,这些操作是根据 @chrisb 的帖子进行的,使用了 test.get_group(5)['col'].median()
2 个回答
1
你可以通过循环来遍历测试内容
test = df.groupby("columnTitle")
for each in test:
print each[0] #columnTitle value
print each[1] #corresponding df equivalent of df[df['columnTitle']==each[0]]
3
像这样吗?gb
是一个分组对象。这个代码会打印出前3个组中的前5行数据。
In [230]: gb = df.groupby(df.index.month)
In [231]: for k in gb.groups.keys()[:3]:
...: print gb.get_group(k)[:5]