本文整理汇总了Python中odps.df.DataFrame.pivot_table方法的典型用法代码示例。如果您正苦于以下问题:Python DataFrame.pivot_table方法的具体用法?Python DataFrame.pivot_table怎么用?Python DataFrame.pivot_table使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类odps.df.DataFrame
的用法示例。
在下文中一共展示了DataFrame.pivot_table方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testPivotTable
# 需要导入模块: from odps.df import DataFrame [as 别名]
# 或者: from odps.df.DataFrame import pivot_table [as 别名]
def testPivotTable(self):
data = [
["name1", 1, 1.0, True],
["name1", 1, 5.0, True],
["name1", 2, 2.0, True],
["name2", 1, 3.0, False],
["name2", 3, 4.0, False],
]
table_name = tn("pyodps_test_mixed_engine_pivot_table")
self.odps.delete_table(table_name, if_exists=True)
table = self.odps.create_table(
name=table_name,
schema=Schema.from_lists(["name", "id", "fid", "ismale"], ["string", "bigint", "double", "boolean"]),
)
expr = DataFrame(table)
try:
self.odps.write_table(table, 0, data)
expr1 = expr.pivot_table(rows="name", values="fid")
res = self.engine.execute(expr1)
result = self._get_result(res)
expected = [["name1", 8.0 / 3], ["name2", 3.5]]
self.assertEqual(sorted(result), sorted(expected))
expr2 = expr.pivot_table(rows="name", values="fid", aggfunc=["mean", "sum"])
res = self.engine.execute(expr2)
result = self._get_result(res)
expected = [["name1", 8.0 / 3, 8.0], ["name2", 3.5, 7.0]]
self.assertEqual(res.schema.names, ["name", "fid_mean", "fid_sum"])
self.assertEqual(sorted(result), sorted(expected))
expr5 = expr.pivot_table(rows="id", values="fid", columns="name", aggfunc=["mean", "sum"])
expr6 = expr5[
"name1_fid_mean",
expr5.groupby(Scalar(1)).sort("name1_fid_mean").name1_fid_mean.astype("float").cumsum(),
]
k = lambda x: list(0 if it is None else it for it in x)
# TODO: fix this situation, act different compared to pandas
expected = [[2, 2], [3, 5], [None, None]]
res = self.engine.execute(expr6)
result = self._get_result(res)
self.assertEqual(sorted(result, key=k), sorted(expected, key=k))
expr3 = expr.pivot_table(rows="id", values="fid", columns="name", fill_value=0).distinct()
res = self.engine.execute(expr3)
result = self._get_result(res)
expected = [[1, 3.0, 3.0], [2, 2.0, 0], [3, 0, 4.0]]
self.assertEqual(res.schema.names, ["id", "name1_fid_mean", "name2_fid_mean"])
self.assertEqual(result, expected)
class Agg(object):
def buffer(self):
return [0]
def __call__(self, buffer, val):
buffer[0] += val
def merge(self, buffer, pbuffer):
buffer[0] += pbuffer[0]
def getvalue(self, buffer):
return buffer[0]
aggfuncs = OrderedDict([("my_sum", Agg), ("mean", "mean")])
expr4 = expr.pivot_table(rows="id", values="fid", columns="name", fill_value=0, aggfunc=aggfuncs)
res = self.engine.execute(expr4)
result = self._get_result(res)
expected = [[1, 6.0, 3.0, 3.0, 3.0], [2, 2.0, 0, 2.0, 0], [3, 0, 4.0, 0, 4.0]]
self.assertEqual(
res.schema.names, ["id", "name1_fid_my_sum", "name2_fid_my_sum", "name1_fid_mean", "name2_fid_mean"]
)
self.assertEqual(result, expected)
finally:
table.drop()