【发布时间】:2018-09-27 06:49:12
【问题描述】:
我无法理解 pandas 中的多索引选择。
0 1 2 3
first second third
C one mean 3 4 2 7
std 4 1 7 7
two mean 3 1 4 7
std 5 6 7 0
three mean 7 0 2 5
std 7 3 7 1
H one mean 2 4 3 3
std 5 5 3 5
two mean 5 7 0 6
std 0 1 0 2
three mean 5 2 5 1
std 9 0 4 6
V one mean 3 7 3 9
std 8 7 9 3
two mean 1 9 9 0
std 1 1 5 1
three mean 3 1 0 6
std 6 2 7 4
我需要创建新行:
- 'CH' : ['CH',:,'mean'] => ['C',:,'mean'] - ['H',:,'mean']
- 'CH' : ['CH',:,'std'] => (['C',:,'std']**2 + ['H',:,'std']**2)**.5
当尝试选择行时,我得到不同类型的错误: UnsortedIndexError: 'MultiIndex Slicing 要求索引是完全 lexsorted tuple len (3), lexsort depth (1)'
这个操作应该如何进行?
import pandas as pd
import numpy as np
iterables = [['C', 'H', 'V'],
['one','two','three'],
['mean','std']]
midx = pd.MultiIndex.from_product(iterables, names=['first', 'second','third'])
chv = pd.DataFrame(np.random.randint(0,high=10,size=(18,4)), index=midx)
print (chv)
idx = pd.IndexSlice
chv.loc[:,idx['C',:,'mean']]
【问题讨论】:
标签: python pandas multi-index