简体   繁体   中英

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 .

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. Also if the axis parameter does not specify the axis to apply the groupby to, what does it do?

I think your r in df.groupby('r', axis=1).agg(sum) should be 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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