本文整理匯總了Python中pandas.Series.value_counts方法的典型用法代碼示例。如果您正苦於以下問題:Python Series.value_counts方法的具體用法?Python Series.value_counts怎麽用?Python Series.value_counts使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.Series
的用法示例。
在下文中一共展示了Series.value_counts方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_series_groupby_value_counts
# 需要導入模塊: from pandas import Series [as 別名]
# 或者: from pandas.Series import value_counts [as 別名]
def test_series_groupby_value_counts(df, keys, bins, n, m):
def rebuild_index(df):
arr = list(map(df.index.get_level_values, range(df.index.nlevels)))
df.index = MultiIndex.from_arrays(arr, names=df.index.names)
return df
for isort, normalize, sort, ascending, dropna \
in product((False, True), repeat=5):
kwargs = dict(normalize=normalize, sort=sort,
ascending=ascending, dropna=dropna, bins=bins)
gr = df.groupby(keys, sort=isort)
left = gr['3rd'].value_counts(**kwargs)
gr = df.groupby(keys, sort=isort)
right = gr['3rd'].apply(Series.value_counts, **kwargs)
right.index.names = right.index.names[:-1] + ['3rd']
# have to sort on index because of unstable sort on values
left, right = map(rebuild_index, (left, right)) # xref GH9212
tm.assert_series_equal(left.sort_index(), right.sort_index())
示例2: describe
# 需要導入模塊: from pandas import Series [as 別名]
# 或者: from pandas.Series import value_counts [as 別名]
def describe(self):
"""
Describes this Categorical
Returns
-------
description: `DataFrame`
A dataframe with frequency and counts by category.
"""
counts = self.value_counts(dropna=False)
freqs = counts / float(counts.sum())
from pandas.core.reshape.concat import concat
result = concat([counts, freqs], axis=1)
result.columns = ['counts', 'freqs']
result.index.name = 'categories'
return result
示例3: describe
# 需要導入模塊: from pandas import Series [as 別名]
# 或者: from pandas.Series import value_counts [as 別名]
def describe(self):
""" Describes this Categorical
Returns
-------
description: `DataFrame`
A dataframe with frequency and counts by category.
"""
counts = self.value_counts(dropna=False)
freqs = counts / float(counts.sum())
from pandas.core.reshape.concat import concat
result = concat([counts, freqs], axis=1)
result.columns = ['counts', 'freqs']
result.index.name = 'categories'
return result
示例4: value_counts
# 需要導入模塊: from pandas import Series [as 別名]
# 或者: from pandas.Series import value_counts [as 別名]
def value_counts(self, dropna=True):
"""
Returns a Series containing counts of each category.
Every category will have an entry, even those with a count of 0.
Parameters
----------
dropna : boolean, default True
Don't include counts of NaN.
Returns
-------
counts : Series
See Also
--------
Series.value_counts
"""
from numpy import bincount
from pandas import Series, CategoricalIndex
code, cat = self._codes, self.categories
ncat, mask = len(cat), 0 <= code
ix, clean = np.arange(ncat), mask.all()
if dropna or clean:
obs = code if clean else code[mask]
count = bincount(obs, minlength=ncat or None)
else:
count = bincount(np.where(mask, code, ncat))
ix = np.append(ix, -1)
ix = self._constructor(ix, dtype=self.dtype,
fastpath=True)
return Series(count, index=CategoricalIndex(ix), dtype='int64')
示例5: value_counts
# 需要導入模塊: from pandas import Series [as 別名]
# 或者: from pandas.Series import value_counts [as 別名]
def value_counts(self, dropna=True):
"""
Returns a Series containing counts of each category.
Every category will have an entry, even those with a count of 0.
Parameters
----------
dropna : boolean, default True
Don't include counts of NaN, even if NaN is a category.
Returns
-------
counts : Series
See Also
--------
Series.value_counts
"""
from numpy import bincount
from pandas import isna, Series, CategoricalIndex
obj = (self.remove_categories([np.nan]) if dropna and
isna(self.categories).any() else self)
code, cat = obj._codes, obj.categories
ncat, mask = len(cat), 0 <= code
ix, clean = np.arange(ncat), mask.all()
if dropna or clean:
obs = code if clean else code[mask]
count = bincount(obs, minlength=ncat or None)
else:
count = bincount(np.where(mask, code, ncat))
ix = np.append(ix, -1)
ix = self._constructor(ix, dtype=self.dtype,
fastpath=True)
return Series(count, index=CategoricalIndex(ix), dtype='int64')
示例6: value_counts
# 需要導入模塊: from pandas import Series [as 別名]
# 或者: from pandas.Series import value_counts [as 別名]
def value_counts(self, dropna=True):
"""
Returns a Series containing counts of each category.
Every category will have an entry, even those with a count of 0.
Parameters
----------
dropna : boolean, default True
Don't include counts of NaN.
Returns
-------
counts : Series
See Also
--------
Series.value_counts
"""
from pandas import Index, Series
# compute counts on the data with no nans
data = self._data[~self._mask]
value_counts = Index(data).value_counts()
array = value_counts.values
# TODO(extension)
# if we have allow Index to hold an ExtensionArray
# this is easier
index = value_counts.index.astype(object)
# if we want nans, count the mask
if not dropna:
# TODO(extension)
# appending to an Index *always* infers
# w/o passing the dtype
array = np.append(array, [self._mask.sum()])
index = Index(np.concatenate(
[index.values,
np.array([np.nan], dtype=object)]), dtype=object)
return Series(array, index=index)
示例7: test_series_groupby_value_counts
# 需要導入模塊: from pandas import Series [as 別名]
# 或者: from pandas.Series import value_counts [as 別名]
def test_series_groupby_value_counts(n, m):
np.random.seed(1234)
def rebuild_index(df):
arr = list(map(df.index.get_level_values, range(df.index.nlevels)))
df.index = MultiIndex.from_arrays(arr, names=df.index.names)
return df
def check_value_counts(df, keys, bins):
for isort, normalize, sort, ascending, dropna \
in product((False, True), repeat=5):
kwargs = dict(normalize=normalize, sort=sort,
ascending=ascending, dropna=dropna, bins=bins)
gr = df.groupby(keys, sort=isort)
left = gr['3rd'].value_counts(**kwargs)
gr = df.groupby(keys, sort=isort)
right = gr['3rd'].apply(Series.value_counts, **kwargs)
right.index.names = right.index.names[:-1] + ['3rd']
# have to sort on index because of unstable sort on values
left, right = map(rebuild_index, (left, right)) # xref GH9212
tm.assert_series_equal(left.sort_index(), right.sort_index())
def loop(df):
bins = None, np.arange(0, max(5, df['3rd'].max()) + 1, 2)
keys = '1st', '2nd', ('1st', '2nd')
for k, b in product(keys, bins):
check_value_counts(df, k, b)
days = date_range('2015-08-24', periods=10)
frame = DataFrame({
'1st': np.random.choice(
list('abcd'), n),
'2nd': np.random.choice(days, n),
'3rd': np.random.randint(1, m + 1, n)
})
loop(frame)
frame.loc[1::11, '1st'] = np.nan
frame.loc[3::17, '2nd'] = np.nan
frame.loc[7::19, '3rd'] = np.nan
frame.loc[8::19, '3rd'] = np.nan
frame.loc[9::19, '3rd'] = np.nan
loop(frame)