我决定开始使用熊猫,所以现在我正在处理MultiIndexed DataFrames。在处理索引和切片时,我注意到以下内容,我无法解释。
import pandas as pd
import numpy as np
# generate a MultiIndexed DataFrame with more or less random data
index = pd.MultiIndex.from_product([[2013,2014],[1,2]],names=['year','visit'])
columns = pd.MultiIndex.from_product([['Bob','Guido','Sue'],['HR','Temp']],names=['subject','type'])
data = np.round(np.random.randn(4,6),1)
data[:,::2] *= 10
data += 37
health_data = pd.DataFrame(data,index=index,columns=columns)
health_data
>>>
subject Bob Guido Sue
type HR Temp HR Temp HR Temp
year visit
2013 1 42.0 37.4 34.0 37.2 41.0 36.2
2 51.0 36.5 34.0 35.7 38.0 37.2
2014 1 52.0 37.2 32.0 37.9 28.0 39.3
2 40.0 36.3 68.0 36.3 55.0 38.3
# Now why is this even working?
>>> health_data.loc[[2014,100]]
subject Bob Guido Sue
type HR Temp HR Temp HR Temp
year visit
2014 1 52.0 37.2 32.0 37.9 28.0 39.3
2 40.0 36.3 68.0 36.3 55.0 38.3
我从health_data.loc[[2014]]
开始,它给出了相同的结果,并且反复试验得出了奇怪的结果。那条线到底是什么?为什么起作用?