本文整理匯總了Python中pandas.core.frame.DataFrame.sum方法的典型用法代碼示例。如果您正苦於以下問題:Python DataFrame.sum方法的具體用法?Python DataFrame.sum怎麽用?Python DataFrame.sum使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.core.frame.DataFrame
的用法示例。
在下文中一共展示了DataFrame.sum方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _unstack_frame
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import sum [as 別名]
def _unstack_frame(obj, level):
from pandas.core.internals import BlockManager, make_block
if obj._is_mixed_type:
unstacker = _Unstacker(np.empty(obj.shape, dtype=bool), # dummy
obj.index, level=level,
value_columns=obj.columns)
new_columns = unstacker.get_new_columns()
new_index = unstacker.get_new_index()
new_axes = [new_columns, new_index]
new_blocks = []
mask_blocks = []
for blk in obj._data.blocks:
bunstacker = _Unstacker(blk.values.T, obj.index, level=level,
value_columns=blk.items)
new_items = bunstacker.get_new_columns()
new_values, mask = bunstacker.get_new_values()
mblk = make_block(mask.T, new_items, new_columns)
mask_blocks.append(mblk)
newb = make_block(new_values.T, new_items, new_columns)
new_blocks.append(newb)
result = DataFrame(BlockManager(new_blocks, new_axes))
mask_frame = DataFrame(BlockManager(mask_blocks, new_axes))
return result.ix[:, mask_frame.sum(0) > 0]
else:
unstacker = _Unstacker(obj.values, obj.index, level=level,
value_columns=obj.columns)
return unstacker.get_result()
示例2: describe
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import sum [as 別名]
def describe(self):
"""
Returns a dataframe with frequency and counts by level.
"""
# Hack?
from pandas.core.frame import DataFrame
counts = DataFrame({
'labels' : self.labels,
'values' : self.labels }
).groupby('labels').count().squeeze().values
freqs = counts / float(counts.sum())
return DataFrame({
'counts': counts,
'freqs': freqs,
'levels': self.levels
}).set_index('levels')
示例3: describe
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import sum [as 別名]
def describe(self):
""" Describes this Categorical
Returns
-------
description: `DataFrame`
A dataframe with frequency and counts by category.
"""
# Hack?
from pandas.core.frame import DataFrame
counts = DataFrame({
'codes' : self._codes,
'values' : self._codes }
).groupby('codes').count()
freqs = counts / float(counts.sum())
from pandas.tools.merge import concat
result = concat([counts,freqs],axis=1)
result.columns = ['counts','freqs']
# fill in the real categories
check = result.index == -1
if check.any():
# Sort -1 (=NaN) to the last position
index = np.arange(0, len(self.categories)+1, dtype='int64')
index[-1] = -1
result = result.reindex(index)
# build new index
categories = np.arange(0,len(self.categories)+1 ,dtype=object)
categories[:-1] = self.categories
categories[-1] = np.nan
result.index = categories.take(com._ensure_platform_int(result.index))
else:
result.index = self.categories.take(com._ensure_platform_int(result.index))
result = result.reindex(self.categories)
result.index.name = 'categories'
return result
示例4: describe
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import sum [as 別名]
def describe(self):
""" Describes this Categorical
Returns
-------
description: `DataFrame`
A dataframe with frequency and counts by level.
"""
# Hack?
from pandas.core.frame import DataFrame
counts = DataFrame({
'codes' : self._codes,
'values' : self._codes }
).groupby('codes').count()
counts.index = self.levels.take(counts.index)
counts = counts.reindex(self.levels)
freqs = counts / float(counts.sum())
from pandas.tools.merge import concat
result = concat([counts,freqs],axis=1)
result.index.name = 'levels'
result.columns = ['counts','freqs']
return result