Python Pandas:数据透视表:aggfunc串联而不是np.size或np.sum [英] Python Pandas : Pivot table : aggfunc concatenate instead of np.size or np.sum

查看:1181
本文介绍了Python Pandas:数据透视表:aggfunc串联而不是np.size或np.sum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据框中有一些条目,例如:

I have some entries in dataframe like :

name, age, phonenumber
 A,10, Phone1
 A,10,Phone2
 B,21,PhoneB1
 B,21,PhoneB2
 C,23,PhoneC

这是我要通过数据透视表实现的目标:

Here is what I am trying to achieve as result of pivot table:

 name, age, phonenumbers, phonenocount
 A,10, "Phone1,Phone2" , 2
 B,21,  "PhoneB1,PhoneB2", 2
 C,23, "PhoneC" , 1

我正在尝试类似的事情:

I was trying something like :

pd.pivot_table(phonedf, index=['name','age','phonenumbers'], values=['phonenumbers'], aggfunc=np.size)

但是我希望将电话号码作为aggfunc的一部分进行串联. 有什么建议吗?

however I want the phone numbers to be concatenated as part of aggfunc. Any Suggestions ?

推荐答案

您可以在groupby之后使用agg函数:

You can use agg function after the groupby:

df.groupby(['name', 'age'])['phonenumber'].\
    agg({'phonecount': pd.Series.nunique, 
         'phonenumber': lambda x: ','.join(x)
        }
       )

#               phonenumber  phonecount
# name  age     
#    A   10   Phone1,Phone2           2
#    B   21 PhoneB1,PhoneB2           2
#    C   23          PhoneC           1

或者根据@root和@Jon Clements的缩写:

Or a shorter version according to @root and @Jon Clements:

df.groupby(['name', 'age'])['phonenumber'].\
   agg({'phonecount': 'nunique', 'phonenumber': ','.join})

这篇关于Python Pandas:数据透视表:aggfunc串联而不是np.size或np.sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆