PandasPython群

2024-10-01 07:49:21 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图在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

你的建议将不胜感激。在


Tags: idplottypedmcontrollongtypesspecies
2条回答

使用括号:

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

而不是方括号

^{pr2}$

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

您需要()

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

或者:

^{pr2}$

说明:

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

相关问题 更多 >