Pedro Braz Pedro Braz -5 years ago 261
Python Question

Pandas MultiIndex groupby retaining index levels

After research I have found no similar questions on this or any other forum.

I'm grouping a MultiIndex dataframe by its inner level. The thing is, after grouping by I still want to know which were the "chosen values" on this inner index.

So I have something of the sort

df = pd.DataFrame([['A', 1, 3],
['A', 2, 4],
['A', 3, 6],
['B', 1, 9],
['B', 2, 10],
['B', 4, 6]],
columns=pd.Index(['Name', 'Date', 'Value'], name='ColumnName')
).set_index(['Name', 'Date'])

ColumnName Value
Name Date
A 1 3
2 4
3 6
B 1 9
2 10
4 6

What I wanted is

ColumnName Value
Name Date
A 3 6
B 4 6

What I was capable of doing was using this command:


was retrieving this:

ColumnName Value
A 6
B 6

Or, by using this command:


retrieving an error.

Keep in mind that this is a performance sensitive application.

Thoughts ?

EDIT: Meanwhile I did submit a feature request at GitHub

Answer Source

This will get it done:

def get_slice(df):
    l0, l1 = df.index.levels
    b0, b1 = df.index.labels

    n = len(l0)
    myslice = range(n)

    for i in myslice:
        myslice[i] = (l0[i], l1[b1[b0 == i][-1]])

    return df.loc[myslice]



1000 loops, best of 3: 458 ┬Ás per loop
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download