python - Pandas 中的 groupby -Python

标签 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

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

最佳答案

使用括号:

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

代替方括号

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

引用给定的link了解更多详情。

关于python - Pandas 中的 groupby -Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47647464/

相关文章:

python - "Player2"无法执行与 "player"相同的操作

python - Django/Nginx - 提供超过一定大小的媒体文件时禁止错误 403

python - 如何使用 scikit-learn 中的信息增益度量选择 Dataframe 中的最佳特征

python - Pandas 在 groupby.apply(..) 之后删除组列

python - 我如何根据部门汇总员工并使用 groupby pandas 显示每个部门的平均工资?

python - 在Python中的for循环中嵌套列表

python - 是否有一种更简洁的方法来遍历所有二进制 4 元组?

Python-检查列是否包含列表中的值,返回值

python - 将数据帧中的特定列转换为 numpy 数组并与原始数据帧合并

python - Pandas groupby,选择最大值的3个元素,并取每组的均值