Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing values in a Multiindex Series in Python

Tags:

python

pandas

I have a dataframe which I applied a groupby method call indexed off "Classification" and "Applying for FA?".

s = df.groupby("Classification")["Applying for FA?"].value_counts()

The following result is below

Classification  Applying for FA?
FF              N                   172
                Y                    26
TR              N                     1
                Y                     1
Name: Applying for FA?, dtype: int64

Looking at the documentation for value_counts() it returns a series which is the object this information is stored. I want to access the particular values in the right column in order to do conditional probability.

For example, if I need P(FF | N), then my result is 172/frequency of N. How to I access the proper value through the two column names. Basically I am looking for something like

s["FF"]["N"] ... returns 172
s["FF"]["Y"] ... returns 26
like image 453
DaveK Avatar asked Sep 05 '25 21:09

DaveK


1 Answers

This format should work:

s.loc[('FF', 'N')]
like image 97
kudeh Avatar answered Sep 08 '25 12:09

kudeh