Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: get value of an index in multi-index

I have a pandas dataframe :

data = {'id': [1,1,2,3],
        'name': ["Noemie","Elena", "Lize", "Patrick"],
        'value': ["pink", "blue", "red", "yellow" ]}

So I gather all the rows with the id "1" thanks to df.set_index(["id","name"])

My columns id and name become indexes, I wonder if is it possible to get for example the value "Noemie"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Knowing that, before multi-indexing I can have it with

df.loc[0].at["name"]

>Solution :

You can use df.index.get_level_values to get the desired index column you want and do comparison.

df[df.index.get_level_values('name') == 'Noemie']
 
          value
id name        
1  Noemie  pink

Or if you just want the index value

df.index[df.index.get_level_values('name') == 'Noemie'][0][1]
Out[30]: 'Noemie'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading