【问题标题】:groupby in Pandas -PythonPandas 中的 groupby -Python
【发布时间】:2017-12-05 06:35:22
【问题描述】:

当我尝试在我不理解的 groupby 对象中执行操作时收到错误消息。

对于可重现的示例,请考虑以下内容:

import pandas as pd

species_plots_types



 record_id  plot_id plot_type   species_id
    0       1   2   Control NL
    2194    2   3   Long-term Krat Exclosure    NL
    1       3   2   Control DM
    4022    4   7   Rodent Exclosure    DM
    2195    5   3   Long-term Krat Exclosure    DM
    4838    6   1   Spectab exclosure   PF
    2       7   2   Control PE
    4839    8   1   Spectab exclosure   DM
    4840    9   1   Spectab exclosure   DM
    6833    10  6   Short-term Krat Exclosure   PF
    8415    11  5   Rodent Exclosure    DS
    4023    12  7   Rodent Exclosure    DM
    2196    13  3   Long-term Krat Exclosure    DM
    9609    14  8   Control DM
    6834    15  6   Short-term Krat Exclosure   DM

species_plots_types.groupby["plot_type"].size().to_frame()

TypeError: 'method' object is not subscriptable

species_plots_types.groupby["plot_type"].count()

TypeError: 'method' object is not subscriptable

您的建议将不胜感激。

【问题讨论】:

    标签: python pandas pandas-groupby


    【解决方案1】:

    使用括号:

    species_plots_types.groupby("plot_type").count() 
    

    代替方括号

    species_plots_types.groupby["plot_type"].count()
    

    有关详细信息,请参阅给定的link

    【讨论】:

      【解决方案2】:

      你需要():

      species_plots_types.groupby("plot_type").size().to_frame()
      

      或者:

      species_plots_types.groupby("plot_type").count()
      

      解释:

      groupby["plot_type"] 表示您在groupby 上调用__getitem__,但由于未定义此方法,因此您将获得TypeError: 'type' object is not subscriptable。有关更多详细信息,请查看 getitem 的 python 文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-30
        • 1970-01-01
        • 2013-07-14
        • 2013-09-23
        • 2014-11-23
        • 2020-10-05
        相关资源
        最近更新 更多