【问题标题】:Rename result from aggregate function in Pandas重命名 Pandas 中聚合函数的结果
【发布时间】:2020-05-01 18:50:32
【问题描述】:

有人能帮我弄清楚如何重命名这些突出显示的项目吗? “Count”应该是(Most popular Item),“sum”应该是“most popular Item”

这是我的代码

most_popular_item = Heroes_file.groupby(["Item ID", "Item Name","Price"]).agg({"Price": ['count','sum']})

【问题讨论】:

  • df["most popular item"] = ... ?

标签: python pandas jupyter-notebook jupyter


【解决方案1】:

要重命名您的 pandas 列,您可以使用 rename

这样,

most_popular_item.columns = most_popular_item.columns.droplevel(0)

将通过删除最外层来删除“价格”。

然后你可以简单地使用重命名

most_popular_item.rename({'count': 'not_count', 'sum': 'not_sum'}, axis=1, inplace=True) 

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 1970-01-01
    • 2017-11-21
    • 2020-02-02
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多