本文整理匯總了Python中pandas.core.frame.DataFrame.reindex方法的典型用法代碼示例。如果您正苦於以下問題:Python DataFrame.reindex方法的具體用法?Python DataFrame.reindex怎麽用?Python DataFrame.reindex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.core.frame.DataFrame
的用法示例。
在下文中一共展示了DataFrame.reindex方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _concat_frames
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import reindex [as 別名]
def _concat_frames(frames, index, columns=None, axis=0):
if axis == 0:
all_index = [np.asarray(x.index) for x in frames]
new_index = Index(np.concatenate(all_index))
if columns is None:
new_columns = frames[0].columns
else:
new_columns = columns
else:
all_columns = [np.asarray(x.columns) for x in frames]
new_columns = Index(np.concatenate(all_columns))
new_index = index
new_values = np.concatenate([x.values for x in frames], axis=axis)
result = DataFrame(new_values, index=new_index, columns=new_columns)
return result.reindex(index=index, columns=columns)
示例2: _concat_frames
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import reindex [as 別名]
def _concat_frames(frames, index, columns=None, axis=0):
if len(frames) == 1:
return frames[0]
if axis == 0:
new_index = _concat_indexes([x.index for x in frames])
if columns is None:
new_columns = frames[0].columns
else:
new_columns = columns
else:
new_columns = _concat_indexes([x.columns for x in frames])
new_index = index
if frames[0]._is_mixed_type:
new_data = {}
for col in new_columns:
new_data[col] = np.concatenate([x[col].values for x in frames])
return DataFrame(new_data, index=new_index, columns=new_columns)
else:
new_values = np.concatenate([x.values for x in frames], axis=axis)
result = DataFrame(new_values, index=new_index, columns=new_columns)
return result.reindex(index=index, columns=columns)
示例3: describe
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import reindex [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
示例4: transform
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import reindex [as 別名]
def transform(self, func):
"""
For given DataFrame, group index by given mapper function or dict, take
the sub-DataFrame (reindex) for this group and call apply(func)
on this sub-DataFrame. Return a DataFrame of the results for each
key.
Note: this function does not aggregate like groupby/tgroupby,
the results of the given function on the subDataFrame should be another
DataFrame.
Parameters
----------
mapper : function, dict-like, or string
Mapping or mapping function. If string given, must be a column
name in the frame
func : function
Function to apply to each subframe
Note
----
Each subframe is endowed the attribute 'groupName' in case
you need to know which group you are working on.
Example
--------
>>> grouped = df.groupby(lambda x: mapping[x])
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
"""
applied = []
obj = self._get_obj_with_exclusions()
for val, inds in self.primary.indices.iteritems():
subframe = obj.take(inds, axis=self.axis)
subframe.groupName = val
try:
res = subframe.apply(func, axis=self.axis)
except Exception: # pragma: no cover
res = func(subframe)
# broadcasting
if isinstance(res, Series):
if res.index is obj.index:
subframe.T.values[:] = res
else:
subframe.values[:] = res
applied.append(subframe)
else:
applied.append(res)
if self.axis == 0:
all_index = [np.asarray(x.index) for x in applied]
new_index = Index(np.concatenate(all_index))
new_columns = obj.columns
else:
all_columns = [np.asarray(x.columns) for x in applied]
new_columns = Index(np.concatenate(all_columns))
new_index = obj.index
new_values = np.concatenate([x.values for x in applied],
axis=self.axis)
result = DataFrame(new_values, index=new_index, columns=new_columns)
return result.reindex(index=obj.index, columns=obj.columns)