首页 > 解决方案 > AttributeError:“DataFrame”对象没有属性“groupyby”

问题描述

长时间使用 pandas,这是我第一次收到此问题标题所示的错误,我被卡住了,因为我看不出 DataFrame 没有 groupby 功能“可用”的任何原因。 " 已经重新安装了 pandas,甚至查看了正在使用的代码(groupby 在核心模块中定义)。

这就是我正在做的,错误如下所示:

def _bin_by_answer(row):
    collocated_answer = row['colname']
    if collocated_answer <= -1:
        return -1
    elif collocated_answer >= 1:
        return 1
    else:
        return 0

df = pd.read_pickle(somepath)
df['binned'] = df.apply(func=lambda row: _bin_by_answer(row), axis=1)
df_sampled = df.groupyby(by='binned', group_keys=False).apply(lambda grp: grp.sample(n=50))

这是错误:

Traceback (most recent call last):
File "/Users/felix/IdeaProjects/cope/ann4class/exportfromaestoretomturk.py", line 858, in <module>
process_and_export_ps2_inductive()
File "/Users/felix/IdeaProjects/cope/ann4class/exportfromaestoretomturk.py", line 786, in process_and_export_ps2_inductive
df_sampled = df_results.groupyby(by=COL_ANSWER1_COLLOCATED_MAJORITY, group_keys=False).apply(
    File "/Users/felix/anaconda3/envs/cope/lib/python3.7/site-packages/pandas/core/generic.py", line 5179, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'groupyby'

标签: python-3.xpandas

解决方案


正如 ddoGas 所建议的,这个错误的原因是一个错字。所以,我想一般的答案是:如果你正在阅读这个问题,因为你遇到了类似的问题,请仔细检查你写的函数名是否正确。


推荐阅读