从多个索引Pandas'中获取值。数据帧

时间:2018-05-29 21:32:26

标签: python pandas dataframe

从书籍Python Data Science Handbook的以下数据框中, 我试图列出

Hello command not found

我可以访问不同的行和列:

import pandas as pd
import numpy as np


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'])

# mock some data
data = np.round(np.random.randn(4, 6), 1)
data[:, ::2] *= 10
data += 37

# create the DataFrame
health_data = pd.DataFrame(data, index=index, columns=columns)

但是,我无法通过访问显示数据框,即

print( health_data['Guido'])
type          HR  Temp
year visit            
2013 1      32.0  36.4
     2      47.0  37.8
2014 1      38.0  36.0
     2      47.0  37.2

print(health_data)
subject      Bob       Guido         Sue      
type          HR  Temp    HR  Temp    HR  Temp
year visit                                    
2013 1      26.0  35.6  44.0  36.8  32.0  37.0
     2      34.0  34.9  44.0  37.2  46.0  35.5
2014 1      46.0  36.8  45.0  37.9  26.0  37.3
     2      41.0  37.4  30.0  37.1  23.0  37.4

print(health_data.index)
MultiIndex(levels=[[2013, 2014], [1, 2]],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
           names=['year', 'visit'])

print(health_data.loc[2013])
subject   Bob       Guido         Sue      
type       HR  Temp    HR  Temp    HR  Temp
visit                                      
1        26.0  35.6  44.0  36.8  32.0  37.0
2        34.0  34.9  44.0  37.2  46.0  35.5

print(health_data.loc[2013, 1])
subject  type
Bob      HR      26.0
         Temp    35.6
Guido    HR      44.0
         Temp    36.8
Sue      HR      32.0
         Temp    37.0
Name: (2013, 1), dtype: float64

因为我收到此错误

health_data[:, 1]

我该怎么做?

1 个答案:

答案 0 :(得分:1)

对于多指数,xs是一个有用的定位器:

health_data.xs(1, level='visit')

这会返回visit1的数据框视图:

subject   Bob       Guido         Sue      
type       HR  Temp    HR  Temp    HR  Temp
year                                       
2013     25.0  37.2  35.0  37.7  21.0  37.8
2014     31.0  35.3  42.0  37.9  38.0  37.2

如果您仍希望显示visit,请使用drop_level=False

health_data.xs(1, level='visit', drop_level=False)

    subject      Bob       Guido         Sue      
type          HR  Temp    HR  Temp    HR  Temp
year visit                                    
2013 1      25.0  37.2  35.0  37.7  21.0  37.8
2014 1      31.0  35.3  42.0  37.9  38.0  37.2