pandas - 从groupby对象中选择值 [英] Pandas - select values from a groupby object

查看:133
本文介绍了 pandas - 从groupby对象中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pandas groupby 对象, c

I have a pandas groupby object, c:

>>> c.index.names
FrozenList([u'Thing1', u'Thing2', u'Month'])
>>> c.columns
Index([u'Tot'], dtype='object')
>>> c
                           Tot
Thing1 Thing2 Month                
G      P      2012-12-01   0.017640
              2013-01-01   0.012062
              2013-02-01   0.029022
              2013-03-01   0.007593
              2013-04-01   0.004862
              2013-05-01   0.002671
              2013-06-01   0.014895
              2013-07-01   0.029641
              2013-08-01   0.051129
              2013-09-01   0.023913
              2013-10-01   0.061406
              2013-11-01   0.054781
              2014-01-01   0.017115
              2014-02-01   0.011919
H      K      2013-06-01   2.390632
              2013-07-01   7.066034
              2013-08-01   5.426312
              2013-09-01   8.276066
              2013-10-01   5.745811
              2013-11-01   2.250162
              2013-12-01   0.976822
              2014-01-01   1.438316
              2014-02-01   3.507220
       M      2012-06-01   3.050136
              2012-07-01   5.911788
              2012-08-01   2.794381
              2012-09-01   4.418268
              2012-10-01   5.312635
              2012-11-01   1.810977
              2012-12-01   3.097878
              2013-01-01   0.811326
              2013-02-01   3.105154
              2013-03-01   2.384704

如何为选择时间序列值, Thing1 = G Thing2 = P ,用于绘图?

How do you select the timeseries values for Thing1=G and Thing2=P, for plotting?

推荐答案

c.loc[('G', 'P'), :]

然而,这样的索引操作,需要对多索引进行排序(否则它可以给与异常)。你可以这样做:

However, it is possible that for such indexing operations, the multi-index needs to be sorted (otherwise it can give and exception). You can do this with:

c = c.sortlevel()






为了正确, c 是不是一个groupby对象,而是一个DataFrame(你也有熊猫 GroupBy 对象,但它们是 .groupby() call)


And to be correct, c is not a groupby object, but a DataFrame (you also have pandas GroupBy objects, but they are the result of a .groupby() call)

这篇关于 pandas - 从groupby对象中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆