Pandas 中的 groupby -Python

rf7*_*rf7 1 python pandas pandas-groupby

当我尝试在 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
Run Code Online (Sandbox Code Playgroud)

我们将不胜感激您的建议。

小智 5

使用括号:

species_plots_types.groupby("plot_type").count() 
Run Code Online (Sandbox Code Playgroud)

而不是方括号

species_plots_types.groupby["plot_type"].count()
Run Code Online (Sandbox Code Playgroud)

请参阅给定的链接了解更多详细信息。