首页 > 解决方案 > 关于在列表列上使用 groupby 的问题

问题描述

我正在使用 MovieLens 1M 数据集来学习熊猫,我想根据流派列获取一些数据。

我得到的数据框的一行是这样的:

movieid title   genres  rating  userid  gender  age occupation  zipcode timestamp
1000204 2198    Modulations (1998)  [Documentary]   5   5949    M   18  17  47901   958846401
1000205 2703    Broken Vessels (1998)   [Drama] 3   5675    M   35  14  30030   976029116
1000206 2845    White Boys (1999)   [Drama] 1   5780    M   18  17  92886   958153068
1000207 3607    One Little Indian (1973)    [Comedy, Drama, Western]    5   5851    F   18  20  55410   957756608
1000208 2909    Five Wives, Three Secretaries and Me (1998) [Documentary]   4   5938    M   25  1   35401   957273353

我想让我们 df.groupby('genres') 对数据框进行分组,然后得到每种流派的总和以及每种流派的平均评分。

但是,当我使用 df.groupby('genres').mean() 时,出现错误“TypeError: unhashable type:'list'”

请告诉我为什么会发生此错误以及如何在数据为列表的列上使用 groupby。

非常感谢!

标签: pythonpandas-groupby

解决方案


groupby将列表作为参数。尝试 df.groupby(['genres']).mean()


推荐阅读