From the following data frame from the book Python Data Science Handbook, I'm Trying to list
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)
I can access different rows and 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
However, I'm unable to display the data frame by its visit i.e.
health_data[:, 1]
since I get this error
TypeError: '(slice(None, None, None), 1)' is an invalid key
How can I do it?