我在DataFrame中有一些条目,例如:
name, age, phonenumber
A,10, Phone1
A,10,Phone2
B,21,PhoneB1
B,21,PhoneB2
C,23,PhoneC
以下是我想通过数据透视表实现的结果:
name, age, phonenumbers, phonenocount
A,10, "Phone1,Phone2" , 2
B,21, "PhoneB1,PhoneB2", 2
C,23, "PhoneC" , 1
我尝试着做一些类似这样的事情:
pd.pivot_table(phonedf, index=['name','age','phonenumbers'], values=['phonenumbers'], aggfunc=np.size)
然而我希望电话号码作为aggfunc的一部分被连接起来。有什么建议吗?