Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 分组依据值和单位计数_Python 3.x_Pandas_Pandas Groupby - Fatal编程技术网

Python 3.x 分组依据值和单位计数

Python 3.x 分组依据值和单位计数,python-3.x,pandas,pandas-groupby,Python 3.x,Pandas,Pandas Groupby,我的df看起来像那样 session_id page_type 10001_0 a 10001_0 b 10001_0 b 10001_0 b 10001_0 c 10001_0 c 10002_0 a 10002_0 a 10002_0 b 10002_0 b 10002_0 c 10002_0 c 我想按“session_id”分组并计算值('a','b','c')) 作为: 我不关心“计数\页面\类型”列中的类型 它

我的df看起来像那样

session_id page_type
10001_0    a
10001_0    b
10001_0    b
10001_0    b
10001_0    c
10001_0    c
10002_0    a
10002_0    a
10002_0    b
10002_0    b
10002_0    c
10002_0    c
我想按“session_id”分组并计算值('a','b','c')) 作为:

我不关心“计数\页面\类型”列中的类型 它也可以是列表。 聚合在多个列上

agg_dict = ({'uid':'first',
             'request_id':'unique',
             'sso_id':'first',
             'article_id' :['first','last','nunique'],
             'event_time':['min','max'],
             'session_duration':'sum',
             'anonymous_id':['first','nunique'],
             'platform':['first','nunique'],
             'brand':['first','last','nunique'],
             'user_type':['first','last'],
             'page_type':'value_counts'})
df.groupby('session_id').agg(agg_dict)
现在我犯了一个错误

ValueError: cannot insert page_type, already exists
有什么建议吗?
谢谢

value\u counts
返回的不是一行而是一个
pd.Series
,请尝试执行以下操作:

df.groupby('session_id').agg({'page_type':lambda x:x.value_counts().to_dict()})
ValueError: cannot insert page_type, already exists