1

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?

1 Answer 1

1

For multiindices, xs is a useful locator:

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

This returns a view of your dataframe where visit is 1:

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

If you still want visit displayed, use 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.