【发布时间】:2017-08-27 08:48:42
【问题描述】:
我有一个要求,我需要将数据框列的行转换为列,但是在 GROUPBY 之后我遇到了一个问题。 下面是一组 3 个用户,它们的类型可以介于 type1 到 type6 之间。
user_id1 type4
user_id1 type6
user_id1 type1
user_id1 type2
user_id1 type1
user_id1 type6
user_id2 type1
user_id2 type2
user_id2 type2
user_id2 type1
user_id2 type3
user_id2 type4
user_id2 type5
user_id2 type6
user_id2 type2
user_id2 type6
user_id3 type1
user_id3 type2
user_id3 type3
user_id3 type2
我期待的输出是 -
user_id type1 type2 type3 type4 type5 type6
user_id1 2 1 0 1 0 2
user_id2 2 3 1 1 1 2
user_id3 1 2 1 0 0 0
我尝试对类型进行分组并得到计数。但不确定如何转换为列,尤其是缺少的类型应填充为 0。
非常感谢您的宝贵时间。
【问题讨论】:
-
显示你尝试过的代码?
标签: python python-3.x pandas sklearn-pandas