参考URL : https://jakevdp.github.io/PythonDataScienceHandbook/03.05-hierarchical-indexing.html
データフレーム作成
# hierarchical indices and columns
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)
health_data
列のHRのみにアクセス。idxを使わない方法もあるけど、使う方法を覚えといた方が良いかもしれない。
idx = pd.IndexSlice
health_data.loc[idx[:,:],idx[:,'HR']]
行のvisit=1のみにアクセス
idx = pd.IndexSlice
health_data.loc[idx[:,1],idx[:,:]]
Guido列とSue列にアクセス
health_data[['Guido','Sue']]
BobのHRにアクセス
health_data.loc[:,('Bob','HR')]
既存のデータフレームにindexを追加する場合は、以下のように再定義する。
idx = pd.MultiIndex.from_arrays([df['group'],df['name']])
df.index = idx
index名にアクセス
> health_data.index.get_level_values(1)
Int64Index([1, 2, 1, 2], dtype='int64', name='visit')
条件を指定して抽出。※~health_data.index.get_level_values(1).isin(リスト)
でも良い。これは複数条件指定出来る。
idx = health_data.index.get_level_values(1)!=1
health_data.loc[idx,]
条件抽出 複数指定
条件を複数指定出来る。HR列を抽出
idx = pd.IndexSlice
cnd = health_data.columns.get_level_values(1).isin(['HR'])
health_data.loc[idx[:,:],idx[:,cnd]]
subject | Bob | Guido | Sue | |
---|---|---|---|---|
type | HR | HR | HR | |
year | visit | |||
2013 | 1 | 37.0 | 30.0 | 26.0 |
2 | 38.0 | 39.0 | 43.0 | |
2014 | 1 | 26.0 | 33.0 | 22.0 |
2 | 34.0 | 39.0 | 26.0 |
HR列「以外」を抽出
idx = pd.IndexSlice
cnd = ~health_data.columns.get_level_values(1).isin(['HR'])
health_data.loc[idx[:,:],idx[:,cnd]]
subject | Bob | Guido | Sue | |
---|---|---|---|---|
type | Temp | Temp | Temp | |
year | visit | |||
2013 | 1 | 35.9 | 36.2 | 37.8 |
2 | 37.0 | 37.1 | 38.5 | |
2014 | 1 | 38.5 | 37.3 | 36.8 |
2 | 37.1 | 37.2 | 37.1 |