简体   繁体   English

pandas groupby 索引值

[英]pandas groupby index value

Is it possible to groupby an index label (instead of a column label)?是否可以按索引标签(而不是列标签)进行分组? This seems like it should be trivial so perhaps I am missing something.这似乎应该是微不足道的,所以也许我错过了一些东西。

import pandas as pd
import numpy as np
df = pd.DataFrame([['a', 'b', 'c'], 
                   ['a', 'a', 'b'], 
                   ['b', 'b', 'c']],
                  index=['q', 'r', 's'], 
                  columns=['x', 'y', 'z'])
df
    x   y   z
q   a   b   c
r   a   a   b
s   b   b   c

This works as I would expect:这正如我所期望的那样工作:

df.groupby('x', axis=0).agg(sum)

    y   z
x       
a   ba  cb
b   b   c

However this fails然而这失败了

df.groupby('s', axis=1).agg(sum)

With a KeyError .带有KeyError

What I would hope to get out is:我希望得到的是:

s   b   c
q   ab  c
r   aa  b

Is it possible to groupby an index value?是否可以按索引值分组? I realize that I can transpose the table, however I need to perform multiple groupbys and it would be much less prone to errors if I could avoid that.我意识到我可以转置表格,但是我需要执行多个 groupbys,如果我可以避免这种情况,那么出错的可能性就会大大降低。 Also if the axis parameter does not specify the axis to apply the groupby to, what does it do?此外,如果axis参数没有指定要应用 groupby 的轴,它有什么作用?

I think your r in df.groupby('r', axis=1).agg(sum) should be s .我觉得你的rdf.groupby('r', axis=1).agg(sum)应该是s Maybe it's your mistake??也许是你的错??

Anyway,反正,

You can groupby based on index value like below.您可以根据索引值进行分组,如下所示。 (My workaround...) (我的解决方法...)

print df[~df.index.isin(['s'])].groupby(df.loc['s'], axis=1).agg(sum)

s   b  c
q  ab  c
r  aa  b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM