栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

python-创建数据透视表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python-创建数据透视表

我认为这是您想要的:

data = np.array([[ 4057,     8,  1374],      [ 4057,     9,   759],      [ 4057,    11,    96],      [89205,    16,   146],      [89205,    17,   154],      [89205,    18,   244]])rows, row_pos = np.unique(data[:, 0], return_inverse=True)cols, col_pos = np.unique(data[:, 1], return_inverse=True)pivot_table = np.zeros((len(rows), len(cols)), dtype=data.dtype)pivot_table[row_pos, col_pos] = data[:, 2]>>> pivot_tablearray([[1374,  759,   96,    0,    0,    0],       [   0,    0,    0,  146,  154,  244]])>>> rowsarray([ 4057, 89205])>>> colsarray([ 8,  9, 11, 16, 17, 18])

这种方法有一些局限性,主要是,如果您对相同的行/列组合重复输入,则不会将它们加在一起,而只会保留一个(可能是最后一个)。如果您想将它们全部加在一起,尽管有些麻烦,但是您可能会滥用scipy的稀疏模块:

data = np.array([[ 4057,     8,  1374],      [ 4057,     9,   759],      [ 4057,    11,    96],      [89205,    16,   146],      [89205,    17,   154],      [89205,    18,   244],      [ 4057,    11,     4]])rows, row_pos = np.unique(data[:, 0], return_inverse=True)cols, col_pos = np.unique(data[:, 1], return_inverse=True)pivot_table = np.zeros((len(rows), len(cols)), dtype=data.dtype)pivot_table[row_pos, col_pos] = data[:, 2]>>> pivot_table # the element at [0, 2] should be 100!!!array([[1374,  759,    4,    0,    0,    0],       [   0,    0,    0,  146,  154,  244]])import scipy.sparse as spspivot_table = sps.coo_matrix((data[:, 2], (row_pos, col_pos)),       shape=(len(rows), len(cols))).A>>> pivot_table # now repeated elements are added togetherarray([[1374,  759,  100,    0,    0,    0],       [   0,    0,    0,  146,  154,  244]])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626026.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号