我目前有类似的数据集-
customerID store_code mode
BBID_204100102 2655 a
BBID_204100102 2906 b
BBID_204100102 2906 d
BBID_204100150 4986 c
BBID_204100150 4986 a
BBID_204100277 4986 d
BBID_204100310 4986 d
我想要类似的东西
customerID store_code a b c d
0 BBID_204100102 2655 1 0 0 0
1 BBID_204100102 2906 0 1 0 0
2 BBID_204100150 4986 1 0 1 0
3 BBID_204100277 4986 0 0 0 1
4 BBID_204100310 4986 0 0 0 1
首先以客户ud和学生ID为中心,然后以上述方式编码模式。
最佳答案
在两个级别上将get_dummies
与set_index
和max
一起使用:
df = (pd.get_dummies(df.set_index(['customerID','store_code']), prefix='', prefix_sep='')
.max(level=[0,1])
.reset_index())
print (df)
customerID store_code a b c d
0 BBID_204100102 2655 1 0 0 0
1 BBID_204100102 2906 0 1 0 1
2 BBID_204100150 4986 1 0 1 0
3 BBID_204100277 4986 0 0 0 1
4 BBID_204100310 4986 0 0 0 1
关于python - 如何在透视图中获取联接值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46803122/