Issue
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"
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'
Answered By - Kevin Choon Liang Yew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.