【问题标题】:Find top 3 items in each group in a dataset查找数据集中每个组中的前 3 个项目
【发布时间】:2021-12-06 16:29:51
【问题描述】:

有一个电影数据集。我想找出每年排名前 3 的类型(当年电影数量最多的类型)。 数据集摘录如下:

      year      genre  imdb_title_id

 19   1894    Romance              1
 29   1906  Biography              1
 31   1906      Crime              1
 33   1906      Drama              1
 58   1911      Drama              4
 73   1911        War              2
 52   1911  Adventure              1
 60   1911    Fantasy              1
 62   1911    History              1
 83   1912      Drama              5
 87   1912    History              2
 79   1912  Biography              1
 81   1912      Crime              1
 91   1912    Mystery              1
 98   1912        War              1
 108  1913      Drama             11
 106  1913      Crime              4
 110  1913    Fantasy              3
 102  1913  Adventure              2
 113  1913     Horror              2

如何在pandas中进行这种操作?我试过 nlargest 但没有得到正确的结果。 这种情况下的预期输出应该是这样的:

19   1894    Romance              1
29   1906  Biography              1
31   1906      Crime              1
33   1906      Drama              1
58   1911      Drama              4
73   1911        War              2
52   1911  Adventure              1
83   1912      Drama              5
87   1912    History              2
79   1912  Biography              1
108  1913      Drama             11
106  1913      Crime              4
110  1913    Fantasy              3

【问题讨论】:

  • groupby('year')nlargest(3) 提供您想要的输出。您尝试了什么,结果是什么错误?

标签: python pandas data-science


【解决方案1】:

nlargest() 应该“正常工作”,但这里有一些示例代码来处理邪恶索引问题。

top3_idx = df.groupby("year")["imdb_title_id"].nlargest(3).droplevel(0).index
top3_df = df.iloc[top3_idx]

基本上你会得到 nlargest,然后使用索引值来过滤你的数据框。

【讨论】:

    【解决方案2】:

    我认为它有效:

    df = df.sort_values(["imdb_title_id"], ascending=False)
    df = df.groupby("year", as_index=False).agg({"genre": lambda x: list(x)[:3], "imdb_title_id": lambda x: list(x)[:3]})
    result = df.explode("genre", ignore_index=True)
    result["imdb_title_id"] = df.explode("imdb_title_id")["imdb_title_id"].values
    

    但可以找到更好的方法。

    【讨论】:

    • 这是想要的。只需添加一行 result = result[result.imdb_title_id != 0] 即可匹配所需的结果。但是,您能解释一下第二行吗?
    • @AyushGupta,感谢您的接受。在第二行中,我使用 lambda 函数进行聚合以制作相关列值的列表,并根据 imdb_title_id 获得其中的前 3 个值。 DataFrame.agg()DataFrame.groupby() 非常有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2022-01-09
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多