How do I access a pandas groupby dataframe by grouped index?

Question:

Following this example I can create a simple dataframe and groupby

import pandas as pd

# Create a sample data frame
df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'bar', 'bar'],
                   'B': range(5), 'C': range(5)})

# group by 'A' and sum 'B'
gf = df.groupby('A').agg({'B': 'sum'})

The result is the grouped dataframe gf

    B
A   
bar 7
foo 3

I would like to access gf by the grouped indices. Something like…

gf['foo'] returns 3 
gf['bar'] returns 7

I would also like to plot by the grouped indices. Something like…

gf.plot('A', 'B') such that  x=['foo','bar'], y=[3,7]
Asked By: astromonerd

||

Answers:

gf.reset_index(level=0, inplace=True)

gf[gf.A == 'bar']

returns:

     A  B
0  bar  7

Plot:

import matplotlib.pyplot as plt

plt.bar(gf.A, gf.B)
Answered By: LN_P

What about:

import matplotlib.pyplot as plt

for k in gf['B'].index:
    print "{}: {}".format(k, gf['B'].loc[k])

plt.bar(gf['B'].index, map(lambda i: gf['B'].loc[i], gf['B'].index))
plt.show()
Answered By: sjplural

as of the writing of this post, soon to be deprecated, there is a way to access the grouped indices without using reset_index. this is a really jank process and should not be used, but here goes:

df_counts.index.to_series().str[0]

returning

A
bar    b
foo    f
Name: A, dtype: object

this will give you the same as if you reset_index’d the data frame and indexed by that column. specifically, the index after the str indexes through the list of indices, say ['a', 'b']

to access the value in b for this value of a, you could just:

gf[gf.index.to_series().str[0] == 'b']['B']

A
bar    7
Name: B, dtype: int64

and coerce to an int, if you’re sure it’s unique:

int(gf[gf.index.to_series().str[0] == 'b']['B'])

7

keep in mind that this is a terrible way to perform this query, as I’ve found out, and you should very much go with @LN_P’s answer which simply resets the pointlessly created index, allowing you to access the column as normal.

Answered By: Daneolog