熊猫:通过多列进行转置和求和

阿维斯

我有这个数据框:

test = pd.DataFrame({
    'user': [1,2,3,4,5,6,7,8,9],
    'category1': [2,4,0,9,1,4,6,0,1],
    'category2': [1,0,1,3,2,0,0,9,0],
    'topic1': [3,2,1,4,2,0,0,1,2],
    'topic2': [0,0,7,2,1,4,6,0,0],

})

    user    category1   category2   topic1  topic2
0   1       2           1           3       0
1   2       4           0           2       0
2   3       0           1           1       7
3   4       9           3           4       2
4   5       1           2           2       1
5   6       4           0           0       4
6   7       6           0           0       6
7   8       0           9           1       0
8   9       1           0           2       0

它显示了用户访问不同类别和主题的时间。

我需要计算访问特定类别的用户访问特定主题的次数。因此输出应如下所示:

    category    category_count  topic   topic_count
0   category1       27          topic1  13
1   category1       27          topic2  13
2   category2       16          topic1  11
3   category2       16          topic2  10

将感谢您的帮助!

UPD:

我最终提出了这个解决方案,但我仍然认为应该有一种更优雅的方法...

categories = ['category1', 'category2']
topics = ['topic1', 'topic2']

l1 = []
l2 = []
l3 = []
l4 = []
for c in categories:
    for t in topics:
        l1.append(c)
        l2.append(test[c].sum())
        l3.append(t)
        l4.append(test[test[c] > 0][t].sum())

d = {'category':l1,
     'category_count':l2,
    'topic':l3,
    'topic_count':l4}

test_new = pd.DataFrame(d)
test_new
耶斯列尔

使用MultiIndexMultiIndex.from_product

mux = pd.MultiIndex.from_product([['category1','category2'],
                                  ['topic1','topic2']])
print (mux)
MultiIndex(levels=[['category1', 'category2'], 
                   ['topic1', 'topic2']],
           codes=[[0, 0, 1, 1], [0, 1, 0, 1]])

然后退出user列-通过下降或ceate索引:

df = test.set_index('user')
#print (df)

使用DataFrame.reindex由第一ANS第二层次:

df1 = df.reindex(mux, axis=1, level=0)
print (df1)
     category1        category2       
        topic1 topic2    topic1 topic2
user                                  
1            2      2         1      1
2            4      4         0      0
3            0      0         1      1
4            9      9         3      3
5            1      1         2      2
6            4      4         0      0
7            6      6         0      0
8            0      0         9      9
9            1      1         0      0

df2 = df.reindex(mux, axis=1, level=1)
print (df2)
     category1        category2       
        topic1 topic2    topic1 topic2
user                                  
1            3      0         3      0
2            2      0         2      0
3            1      7         1      7
4            4      2         4      2
5            2      1         2      1
6            0      4         0      4
7            0      6         0      6
8            1      0         1      0
9            2      0         2      0

那么可能的和值categories,并通过过滤器DataFrame.whereDataFrame.gtsum的主题:

s1 = df1.sum().rename('category_count')
s2 = df2.where(df1.gt(0)).sum().astype(int).rename('topic_count')

最后加入:

df = (pd.concat([s1, s2], axis=1)
        .rename_axis(('category','topic'))
        .reset_index()
        .sort_index(axis=1))
print (df)
    category  category_count   topic  topic_count
0  category1              27  topic1           13
1  category1              27  topic2           13
2  category2              16  topic1           11
3  category2              16  topic2           10

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章