本文整理汇总了Python中pandas.core.api.MultiIndex类的典型用法代码示例。如果您正苦于以下问题:Python MultiIndex类的具体用法?Python MultiIndex怎么用?Python MultiIndex使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MultiIndex类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_loc_getitem_int_slice
def test_loc_getitem_int_slice(self):
# int slices in int
self.check_result('int slice1', 'loc', slice(2,4), 'ix', { 0 : [2,4], 1: [3,6], 2: [4,8] }, typs = ['ints'], fails=KeyError)
# ok
self.check_result('int slice2', 'loc', slice(2,4), 'ix', [2,4], typs = ['ints'], axes = 0)
self.check_result('int slice2', 'loc', slice(3,6), 'ix', [3,6], typs = ['ints'], axes = 1)
self.check_result('int slice2', 'loc', slice(4,8), 'ix', [4,8], typs = ['ints'], axes = 2)
# GH 3053
# loc should treat integer slices like label slices
from itertools import product
index = MultiIndex.from_tuples([t for t in product([6,7,8], ['a', 'b'])])
df = DataFrame(np.random.randn(6, 6), index, index)
result = df.loc[6:8,:]
expected = df.ix[6:8,:]
assert_frame_equal(result,expected)
index = MultiIndex.from_tuples([t for t in product([10, 20, 30], ['a', 'b'])])
df = DataFrame(np.random.randn(6, 6), index, index)
result = df.loc[20:30,:]
expected = df.ix[20:30,:]
assert_frame_equal(result,expected)
# doc examples
result = df.loc[10,:]
expected = df.ix[10,:]
assert_frame_equal(result,expected)
result = df.loc[:,10]
#expected = df.ix[:,10] (this fails)
expected = df[10]
assert_frame_equal(result,expected)
示例2: test_mi_access
def test_mi_access(self):
# GH 4145
data = """h1 main h3 sub h5
0 a A 1 A1 1
1 b B 2 B1 2
2 c B 3 A1 3
3 d A 4 B2 4
4 e A 5 B2 5
5 f B 6 A2 6
"""
df = pd.read_csv(StringIO(data),sep='\s+',index_col=0)
df2 = df.set_index(['main', 'sub']).T.sort_index(1)
index = Index(['h1','h3','h5'])
columns = MultiIndex.from_tuples([('A','A1')],names=['main','sub'])
expected = DataFrame([['a',1,1]],index=columns,columns=index).T
result = df2.loc[:,('A','A1')]
assert_frame_equal(result,expected)
result = df2[('A','A1')]
assert_frame_equal(result,expected)
# GH 4146, not returning a block manager when selecting a unique index
# from a duplicate index
expected = DataFrame([['a',1,1]],index=['A1'],columns=['h1','h3','h5'],).T
result = df2['A']['A1']
assert_frame_equal(result,expected)
# selecting a non_unique from the 2nd level
expected = DataFrame([['d',4,4],['e',5,5]],index=Index(['B2','B2'],name='sub'),columns=['h1','h3','h5'],).T
result = df2['A']['B2']
assert_frame_equal(result,expected)
示例3: test_xs_multiindex
def test_xs_multiindex(self):
# GH2903
columns = MultiIndex.from_tuples([('a', 'foo'), ('a', 'bar'), ('b', 'hello'), ('b', 'world')], names=['lvl0', 'lvl1'])
df = DataFrame(np.random.randn(4, 4), columns=columns)
df.sortlevel(axis=1,inplace=True)
result = df.xs('a', level='lvl0', axis=1)
expected = df.iloc[:,0:2].loc[:,'a']
assert_frame_equal(result,expected)
示例4: test_xs_multiindex
def test_xs_multiindex(self):
# GH2903
columns = MultiIndex.from_tuples(
[("a", "foo"), ("a", "bar"), ("b", "hello"), ("b", "world")], names=["lvl0", "lvl1"]
)
df = DataFrame(np.random.randn(4, 4), columns=columns)
df.sortlevel(axis=1, inplace=True)
result = df.xs("a", level="lvl0", axis=1)
expected = df.iloc[:, 0:2].loc[:, "a"]
assert_frame_equal(result, expected)
result = df.xs("foo", level="lvl1", axis=1)
expected = df.iloc[:, 1:2].copy()
expected.columns = expected.columns.droplevel("lvl1")
assert_frame_equal(result, expected)
示例5: test_large_mi_dataframe_indexing
def test_large_mi_dataframe_indexing(self):
# GH10645
result = MultiIndex.from_arrays([range(10 ** 6), range(10 ** 6)])
assert (not (10 ** 6, 0) in result)