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


Python Series.nth方法代码示例

本文整理汇总了Python中pandas.Series.nth方法的典型用法代码示例。如果您正苦于以下问题:Python Series.nth方法的具体用法?Python Series.nth怎么用?Python Series.nth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pandas.Series的用法示例。


在下文中一共展示了Series.nth方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_nth_multi_index_as_expected

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_nth_multi_index_as_expected():
    # PR 9090, related to issue 8979
    # test nth on MultiIndex
    three_group = DataFrame(
        {'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar',
               'foo', 'foo', 'foo'],
         'B': ['one', 'one', 'one', 'two', 'one', 'one', 'one', 'two',
               'two', 'two', 'one'],
         'C': ['dull', 'dull', 'shiny', 'dull', 'dull', 'shiny', 'shiny',
               'dull', 'shiny', 'shiny', 'shiny']})
    grouped = three_group.groupby(['A', 'B'])
    result = grouped.nth(0)
    expected = DataFrame(
        {'C': ['dull', 'dull', 'dull', 'dull']},
        index=MultiIndex.from_arrays([['bar', 'bar', 'foo', 'foo'],
                                      ['one', 'two', 'one', 'two']],
                                     names=['A', 'B']))
    assert_frame_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:20,代码来源:test_nth.py

示例2: test_nth_column_order

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_nth_column_order():
    # GH 20760
    # Check that nth preserves column order
    df = DataFrame([[1, 'b', 100],
                    [1, 'a', 50],
                    [1, 'a', np.nan],
                    [2, 'c', 200],
                    [2, 'd', 150]],
                   columns=['A', 'C', 'B'])
    result = df.groupby('A').nth(0)
    expected = DataFrame([['b', 100.0],
                          ['c', 200.0]],
                         columns=['C', 'B'],
                         index=Index([1, 2], name='A'))
    assert_frame_equal(result, expected)

    result = df.groupby('A').nth(-1, dropna='any')
    expected = DataFrame([['a', 50.0],
                          ['d', 150.0]],
                         columns=['C', 'B'],
                         index=Index([1, 2], name='A'))
    assert_frame_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:24,代码来源:test_nth.py

示例3: test_nth_multi_index_as_expected

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_nth_multi_index_as_expected(self):
        # PR 9090, related to issue 8979
        # test nth on MultiIndex
        three_group = DataFrame(
            {'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar',
                   'foo', 'foo', 'foo'],
             'B': ['one', 'one', 'one', 'two', 'one', 'one', 'one', 'two',
                   'two', 'two', 'one'],
             'C': ['dull', 'dull', 'shiny', 'dull', 'dull', 'shiny', 'shiny',
                   'dull', 'shiny', 'shiny', 'shiny']})
        grouped = three_group.groupby(['A', 'B'])
        result = grouped.nth(0)
        expected = DataFrame(
            {'C': ['dull', 'dull', 'dull', 'dull']},
            index=MultiIndex.from_arrays([['bar', 'bar', 'foo', 'foo'],
                                          ['one', 'two', 'one', 'two']],
                                         names=['A', 'B']))
        assert_frame_equal(result, expected) 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:20,代码来源:test_nth.py

示例4: test_first_last_nth_dtypes

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_first_last_nth_dtypes(df_mixed_floats):

    df = df_mixed_floats.copy()
    df['E'] = True
    df['F'] = 1

    # tests for first / last / nth
    grouped = df.groupby('A')
    first = grouped.first()
    expected = df.loc[[1, 0], ['B', 'C', 'D', 'E', 'F']]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(first, expected)

    last = grouped.last()
    expected = df.loc[[5, 7], ['B', 'C', 'D', 'E', 'F']]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(last, expected)

    nth = grouped.nth(1)
    expected = df.loc[[3, 2], ['B', 'C', 'D', 'E', 'F']]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(nth, expected)

    # GH 2763, first/last shifting dtypes
    idx = lrange(10)
    idx.append(9)
    s = Series(data=lrange(11), index=idx, name='IntCol')
    assert s.dtype == 'int64'
    f = s.groupby(level=0).first()
    assert f.dtype == 'int64' 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:35,代码来源:test_nth.py

示例5: test_nth_multi_index

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_nth_multi_index(three_group):
    # PR 9090, related to issue 8979
    # test nth on MultiIndex, should match .first()
    grouped = three_group.groupby(['A', 'B'])
    result = grouped.nth(0)
    expected = grouped.first()
    assert_frame_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:9,代码来源:test_nth.py

示例6: test_group_selection_cache

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_group_selection_cache():
    # GH 12839 nth, head, and tail should return same result consistently
    df = DataFrame([[1, 2], [1, 4], [5, 6]], columns=['A', 'B'])
    expected = df.iloc[[0, 2]].set_index('A')

    g = df.groupby('A')
    result1 = g.head(n=2)
    result2 = g.nth(0)
    assert_frame_equal(result1, df)
    assert_frame_equal(result2, expected)

    g = df.groupby('A')
    result1 = g.tail(n=2)
    result2 = g.nth(0)
    assert_frame_equal(result1, df)
    assert_frame_equal(result2, expected)

    g = df.groupby('A')
    result1 = g.nth(0)
    result2 = g.head(n=2)
    assert_frame_equal(result1, expected)
    assert_frame_equal(result2, df)

    g = df.groupby('A')
    result1 = g.nth(0)
    result2 = g.tail(n=2)
    assert_frame_equal(result1, expected)
    assert_frame_equal(result2, df) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:30,代码来源:test_nth.py

示例7: test_first_last_nth_dtypes

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_first_last_nth_dtypes(self):

        df = self.df_mixed_floats.copy()
        df['E'] = True
        df['F'] = 1

        # tests for first / last / nth
        grouped = df.groupby('A')
        first = grouped.first()
        expected = df.loc[[1, 0], ['B', 'C', 'D', 'E', 'F']]
        expected.index = Index(['bar', 'foo'], name='A')
        expected = expected.sort_index()
        assert_frame_equal(first, expected)

        last = grouped.last()
        expected = df.loc[[5, 7], ['B', 'C', 'D', 'E', 'F']]
        expected.index = Index(['bar', 'foo'], name='A')
        expected = expected.sort_index()
        assert_frame_equal(last, expected)

        nth = grouped.nth(1)
        expected = df.loc[[3, 2], ['B', 'C', 'D', 'E', 'F']]
        expected.index = Index(['bar', 'foo'], name='A')
        expected = expected.sort_index()
        assert_frame_equal(nth, expected)

        # GH 2763, first/last shifting dtypes
        idx = lrange(10)
        idx.append(9)
        s = Series(data=lrange(11), index=idx, name='IntCol')
        assert s.dtype == 'int64'
        f = s.groupby(level=0).first()
        assert f.dtype == 'int64' 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:35,代码来源:test_nth.py

示例8: test_nth_multi_index

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_nth_multi_index(self):
        # PR 9090, related to issue 8979
        # test nth on MultiIndex, should match .first()
        grouped = self.three_group.groupby(['A', 'B'])
        result = grouped.nth(0)
        expected = grouped.first()
        assert_frame_equal(result, expected) 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:9,代码来源:test_nth.py

示例9: test_nth_empty

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_nth_empty():
    # GH 16064
    df = DataFrame(index=[0], columns=['a', 'b', 'c'])
    result = df.groupby('a').nth(10)
    expected = DataFrame(index=Index([], name='a'), columns=['b', 'c'])
    assert_frame_equal(result, expected)

    result = df.groupby(['a', 'b']).nth(10)
    expected = DataFrame(index=MultiIndex([[], []], [[], []],
                                          names=['a', 'b']),
                         columns=['c'])
    assert_frame_equal(result, expected) 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:14,代码来源:test_nth.py

示例10: test_first_last_nth

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_first_last_nth(df):
    # tests for first / last / nth
    grouped = df.groupby('A')
    first = grouped.first()
    expected = df.loc[[1, 0], ['B', 'C', 'D']]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(first, expected)

    nth = grouped.nth(0)
    assert_frame_equal(nth, expected)

    last = grouped.last()
    expected = df.loc[[5, 7], ['B', 'C', 'D']]
    expected.index = Index(['bar', 'foo'], name='A')
    assert_frame_equal(last, expected)

    nth = grouped.nth(-1)
    assert_frame_equal(nth, expected)

    nth = grouped.nth(1)
    expected = df.loc[[2, 3], ['B', 'C', 'D']].copy()
    expected.index = Index(['foo', 'bar'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(nth, expected)

    # it works!
    grouped['B'].first()
    grouped['B'].last()
    grouped['B'].nth(0)

    df.loc[df['A'] == 'foo', 'B'] = np.nan
    assert isna(grouped['B'].first()['foo'])
    assert isna(grouped['B'].last()['foo'])
    assert isna(grouped['B'].nth(0)['foo'])

    # v0.14.0 whatsnew
    df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
    g = df.groupby('A')
    result = g.first()
    expected = df.iloc[[1, 2]].set_index('A')
    assert_frame_equal(result, expected)

    expected = df.iloc[[1, 2]].set_index('A')
    result = g.nth(0, dropna='any')
    assert_frame_equal(result, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:48,代码来源:test_nth.py

示例11: test_first_last_nth

# 需要导入模块: from pandas import Series [as 别名]
# 或者: from pandas.Series import nth [as 别名]
def test_first_last_nth(self):
        # tests for first / last / nth
        grouped = self.df.groupby('A')
        first = grouped.first()
        expected = self.df.loc[[1, 0], ['B', 'C', 'D']]
        expected.index = Index(['bar', 'foo'], name='A')
        expected = expected.sort_index()
        assert_frame_equal(first, expected)

        nth = grouped.nth(0)
        assert_frame_equal(nth, expected)

        last = grouped.last()
        expected = self.df.loc[[5, 7], ['B', 'C', 'D']]
        expected.index = Index(['bar', 'foo'], name='A')
        assert_frame_equal(last, expected)

        nth = grouped.nth(-1)
        assert_frame_equal(nth, expected)

        nth = grouped.nth(1)
        expected = self.df.loc[[2, 3], ['B', 'C', 'D']].copy()
        expected.index = Index(['foo', 'bar'], name='A')
        expected = expected.sort_index()
        assert_frame_equal(nth, expected)

        # it works!
        grouped['B'].first()
        grouped['B'].last()
        grouped['B'].nth(0)

        self.df.loc[self.df['A'] == 'foo', 'B'] = np.nan
        assert isna(grouped['B'].first()['foo'])
        assert isna(grouped['B'].last()['foo'])
        assert isna(grouped['B'].nth(0)['foo'])

        # v0.14.0 whatsnew
        df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
        g = df.groupby('A')
        result = g.first()
        expected = df.iloc[[1, 2]].set_index('A')
        assert_frame_equal(result, expected)

        expected = df.iloc[[1, 2]].set_index('A')
        result = g.nth(0, dropna='any')
        assert_frame_equal(result, expected) 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:48,代码来源:test_nth.py


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