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


Python Series.value_counts方法代码示例

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


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

示例1: test_categorical_nans

# 需要导入模块: from pandas.core.api import Series [as 别名]
# 或者: from pandas.core.api.Series import value_counts [as 别名]
    def test_categorical_nans(self):
        s = Series(pd.Categorical(list('aaaaabbbcc')))  # 4,3,2,1 (nan)
        s.iloc[1] = np.nan
        result = s.value_counts()
        expected = pd.Series([4, 3, 2], index=pd.CategoricalIndex(
            ['a', 'b', 'c'], categories=['a', 'b', 'c']))
        tm.assert_series_equal(result, expected, check_index_type=True)
        result = s.value_counts(dropna=False)
        expected = pd.Series([
            4, 3, 2, 1
        ], index=pd.CategoricalIndex(['a', 'b', 'c', np.nan]))
        tm.assert_series_equal(result, expected, check_index_type=True)

        # out of order
        s = Series(pd.Categorical(
            list('aaaaabbbcc'), ordered=True, categories=['b', 'a', 'c']))
        s.iloc[1] = np.nan
        result = s.value_counts()
        expected = pd.Series([4, 3, 2], index=pd.CategoricalIndex(
            ['a', 'b', 'c'], categories=['b', 'a', 'c'], ordered=True))
        tm.assert_series_equal(result, expected, check_index_type=True)

        result = s.value_counts(dropna=False)
        expected = pd.Series([4, 3, 2, 1], index=pd.CategoricalIndex(
            ['a', 'b', 'c', np.nan], categories=['b', 'a', 'c'], ordered=True))
        tm.assert_series_equal(result, expected, check_index_type=True)
开发者ID:DLlearn,项目名称:pandas,代码行数:28,代码来源:test_algos.py

示例2: test_categorical

# 需要导入模块: from pandas.core.api import Series [as 别名]
# 或者: from pandas.core.api.Series import value_counts [as 别名]
    def test_categorical(self):
        s = Series(pd.Categorical(list('aaabbc')))
        result = s.value_counts()
        expected = pd.Series([3, 2, 1], index=pd.CategoricalIndex(['a', 'b', 'c']))
        tm.assert_series_equal(result, expected, check_index_type=True)

        # preserve order?
        s = s.cat.as_ordered()
        result = s.value_counts()
        expected.index = expected.index.as_ordered()
        tm.assert_series_equal(result, expected, check_index_type=True)
开发者ID:ajcr,项目名称:pandas,代码行数:13,代码来源:test_algos.py

示例3: test_categorical_zeroes

# 需要导入模块: from pandas.core.api import Series [as 别名]
# 或者: from pandas.core.api.Series import value_counts [as 别名]
 def test_categorical_zeroes(self):
     # keep the `d` category with 0
     s = Series(pd.Categorical(
         list('bbbaac'), categories=list('abcd'), ordered=True))
     result = s.value_counts()
     expected = Series([3, 2, 1, 0], index=pd.Categorical(
         ['b', 'a', 'c', 'd'], categories=list('abcd'), ordered=True))
     tm.assert_series_equal(result, expected, check_index_type=True)
开发者ID:DLlearn,项目名称:pandas,代码行数:10,代码来源:test_algos.py


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