1

I have a pandas table in the following format [df], indexed by 'noc' and 'year'. How can I access a 'noc, year combination' and save the entry of 'total_medals' to a list?

          medal  Bronze  Gold  Medal  Silver  total_medals 
noc year                                                 
ALG 1984    2.0     NaN   NaN    NaN     2.0    2.000000 
    1992    4.0     2.0   NaN    NaN     6.0    4.000000 
    1996    2.0     1.0   NaN    4.0     7.0    5.000000 
ANZ 1984    2.0    15.0   NaN    2.0    19.0   19.000000 
    1992    3.0     5.0   NaN    2.0    10.0   14.500000 
    1996    1.0     2.0   NaN    2.0     5.0   11.333333 
ARG 1984    2.0     6.0   NaN    3.0    11.0   11.000000 
    1992    5.0     3.0   NaN   24.0    32.0   21.500000 
    1996    3.0     7.0   NaN    5.0    15.0   19.333333

For example: I want to acccess the 'total_medals' of ARG in 1992 (which is 21.5) ans save this to a new list.

Dominik Scheld
  • 125
  • 2
  • 9
  • Can you explain why is necessary list? Because output is scalar. – jezrael Oct 31 '19 at 08:30
  • 1
    Does this answer your question? [Select rows in pandas MultiIndex DataFrame](https://stackoverflow.com/questions/53927460/select-rows-in-pandas-multiindex-dataframe) – Mykola Zotko Oct 31 '19 at 08:35

1 Answers1

1

There is MultiIndex in index values, so you can select values by tuples in DataFrame.loc:

a = df.loc[('ARG',1992), 'total_medals']
print (a)
21.5
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252