当前位置: 首页>>代码示例>>Python>>正文


Python MultiIndex.from_tuples方法代码示例

本文整理汇总了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)
开发者ID:ptmahent,项目名称:pandas,代码行数:37,代码来源:test_indexing.py


示例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)
开发者ID:rgbkrk,项目名称:pandas,代码行数:36,代码来源:test_indexing.py


示例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)
开发者ID:XiaonuoGantan,项目名称:pandas,代码行数:11,代码来源:test_indexing.py


示例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)
开发者ID:ronenabr,项目名称:pandas,代码行数:18,代码来源:test_indexing.py



注:本文中的pandas.core.api.MultiIndex.from_tuples方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。