本文整理匯總了Python中pandas.core.frame.DataFrame.unstack方法的典型用法代碼示例。如果您正苦於以下問題:Python DataFrame.unstack方法的具體用法?Python DataFrame.unstack怎麽用?Python DataFrame.unstack使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.core.frame.DataFrame
的用法示例。
在下文中一共展示了DataFrame.unstack方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: pivot
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import unstack [as 別名]
def pivot(self, index=None, columns=None, values=None):
"""
See DataFrame.pivot
"""
index_vals = self[index]
column_vals = self[columns]
mindex = MultiIndex.from_arrays([index_vals, column_vals],
names=[index, columns])
if values is None:
items = self.columns - [index, columns]
mat = self.reindex(columns=items).values
else:
items = [values]
mat = np.atleast_2d(self[values].values).T
stacked = DataFrame(mat, index=mindex, columns=items)
if not mindex.is_lexsorted():
stacked = stacked.sortlevel(level=0)
unstacked = stacked.unstack()
if values is not None:
unstacked.columns = unstacked.columns.droplevel(0)
return unstacked
示例2: pivot
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import unstack [as 別名]
def pivot(self, index=None, columns=None, values=None):
"""
See DataFrame.pivot
"""
index_vals = self[index]
column_vals = self[columns]
mindex = MultiIndex.from_arrays([index_vals, column_vals])
try:
mindex._verify_integrity()
except Exception:
raise Exception("duplicate index/column pairs!")
if values is None:
items = self.columns - [index, columns]
mat = self.reindex(columns=items).values
else:
items = [values]
mat = np.atleast_2d(self[values].values).T
stacked = DataFrame(mat, index=mindex, columns=items)
if not mindex.is_lexsorted():
stacked = stacked.sortlevel(level=0)
unstacked = stacked.unstack()
if values is not None:
unstacked.columns = unstacked.columns.droplevel(0)
return unstacked