与熊猫的复杂聚集

时间:2020-03-22 14:18:39

标签: python pandas

假设我有一张这样的桌子:

column_1 column_2
a        10
b        20
c        10
a        10

我需要创建这样的表:

   10    20
a  2     0
b  0     1
c  1     0

因此column_1的唯一值必须是索引,而column_2的唯一值必须是列。新表的值是第一个表中的行数,其中某些对值在column_1和column_2中。 我想象的方式是为column_2的每个唯一值创建一个新列,然后使用.groupby()。像这样:

column_1 column_2   10     20
    a        10     True   False
    b        20     False  True
    c        10     True   False
    a        10     True   False

然后table.drop('column_2',axis=1).groupby('column_1').aggregate(sum) 但是,目前正在使用的数据集的最终表的大小将为2500x744,而笔记本电脑在计算所有数据时只会冻结。

是否有更有效的方法来获取我需要的表?

0 个答案:

没有答案
相关问题