我有以下格式[df]的熊猫表,分别由“ noc”和“ year”索引。如何访问“ noc,年份组合”并将“ total_medals”条目保存到列表中?
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
例如:我想在1992年访问ARG的“ total_medals”(即21.5)并将其保存到新列表中。
有MultiIndex
索引值,因此您可以在中按元组选择值DataFrame.loc
:
a = df.loc[('ARG',1992), 'total_medals']
print (a)
21.5
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句