Python Forum
Selecting rows that contain certain values using two alternatives - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Selecting rows that contain certain values using two alternatives (/thread-32058.html)



Selecting rows that contain certain values using two alternatives - tgottsc1 - Jan-18-2021

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!


RE: Selecting rows that contain certain values using two alternatives - nealc - Jan-24-2021

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)