本文整理汇总了Python中pandas.core.api.MultiIndex.from_tuples方法的典型用法代码示例。如果您正苦于以下问题:Python MultiIndex.from_tuples方法的具体用法?Python MultiIndex.from_tuples怎么用?Python MultiIndex.from_tuples使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.api.MultiIndex
的用法示例。
在下文中一共展示了MultiIndex.from_tuples方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_loc_getitem_int_slice
# 需要导入模块: from pandas.core.api import MultiIndex [as 别名]
# 或者: from pandas.core.api.MultiIndex import from_tuples [as 别名]
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
# 需要导入模块: from pandas.core.api import MultiIndex [as 别名]
# 或者: from pandas.core.api.MultiIndex import from_tuples [as 别名]
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
# 需要导入模块: from pandas.core.api import MultiIndex [as 别名]
# 或者: from pandas.core.api.MultiIndex import from_tuples [as 别名]
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
# 需要导入模块: from pandas.core.api import MultiIndex [as 别名]
# 或者: from pandas.core.api.MultiIndex import from_tuples [as 别名]
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)