Python Forum

Full Version: Selecting rows that contain certain values using two alternatives
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I have a dataset and the following code:

a = dataset.set_index('c', append=True).sort_index()
This generates a table with 3 indices (a, b and c) and one column containing values.

The column c includes a couple of characteristics of which I only want to select the rows with "Total".

Therefore, I have used:

dataset.loc[('Total'), :]
However, this generates a KeyError: 'Total'

Alternatively, the code works using the following:

dataset.set_index('c', append=True).sort_index().loc[(slice(None), slice(None), 'Total'), :]
To me it is not clear why it works with the second alternative but not with the first one.

I would be happy if someone could please help me.

Many thanks!
Maybe you're thinking of .xs?

import pandas as pd


if __name__ == "__main__":
    dataset = pd.DataFrame([(1,1,'a',10),(2,2,'b',20),(3,3,'c',30),
        (4,4,"Total",40)],
        columns=["a","b","c","value"])
    dataset.set_index(["a","b","c"], inplace=True)
    dataset.xs("Total",level="c",drop_level=False)