如何修复“pandas.core.common”没有属性“AbstractMethodError”?

我想查看可用于 Pandas 对象的方法。当我运行此代码时,出现 AttributeError 错误。我已经搜索过但没有找到此错误的示例或如何修复它。


for i in (df_jobs.groupby(['group', 'failed'])['failed']):

    object_methods = [method_name for method_name in dir(i[1])

                      if callable(getattr(i[1], method_name))]

    break


AttributeError                            Traceback (most recent call last)

<ipython-input-322-70ac95067677> in <module>

     54 #     print(i[1].count())  # YES YES YES

     55 

---> 56     object_methods = [method_name for method_name in dir(i[1])

     57                       if callable(getattr(i[1], method_name))]

     58     break


<ipython-input-322-70ac95067677> in <listcomp>(.0)

     55 

     56     object_methods = [method_name for method_name in dir(i[1])

---> 57                       if callable(getattr(i[1], method_name))]

     58     break

     59 


~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)

   4374             if self._info_axis._can_hold_identifiers_and_holds_name(name):

   4375                 return self[name]

-> 4376             return object.__getattribute__(self, name)

   4377 

   4378     def __setattr__(self, name, value):


~\Anaconda3\lib\site-packages\pandas\core\generic.py in _constructor_sliced(self)

    222         original, such as DataFrame single columns slicing.

    223         """

--> 224         raise com.AbstractMethodError(self)

    225 

    226     @property


AttributeError: module 'pandas.core.common' has no attribute 'AbstractMethodError'


我本周卸载/重新安装了 Anaconda。

熊猫 0.23.4 py37h830ac7b_0


服务器信息:

您正在使用 Jupyter 笔记本。


笔记本服务器的版本为:5.7.4-f6790e4

服务器在此版本的 Python 上运行:

Python 3.7.1(默认,2018 年 12 月 10 日,22:54:23)[MSC v.1915 64 位 (AMD64) ]


当前内核信息:

Python 3.7.1(默认,2018 年 12 月 10 日,22:54:23)[MSC v.1915 64 位(AMD64)]

IPython 7.2.0——增强的交互式 Python。类型 '?' 求助。


互换的青春
浏览 224回答 1
1回答

牧羊人nacy

好的,您的代码的问题是您如何迭代 group by 的结果。这个答案显示了如何正确迭代。此代码显示了如何完成您的任务:import pandas as pddef method1():&nbsp; &nbsp; return 0exampleDf = pd.DataFrame(columns=["name","group","failed"])exampleDf.loc[0] = ["method1", "failed", 1]exampleDf.loc[1] = ["method2", "success", 0]exampleDf.loc[2] = ["method3", "success", 0]exampleDf.loc[3] = ["method4", "failed", 1]groupedDf = exampleDf.groupby(['group', 'failed'])for params, table in groupedDf:&nbsp; &nbsp; print("Iterating over the table with the params: " + str(params) )&nbsp; &nbsp; print("Table: \n" + str(table))&nbsp; &nbsp; for index, row in table.iterrows():&nbsp; &nbsp; &nbsp; &nbsp; if(row['name'] in dir()):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("The method " + str(row['name']) + " is defined.")输出:Iterating over the table with the params: ('failed', 1)Table:&nbsp;&nbsp; &nbsp; &nbsp; name&nbsp; &nbsp;group failed0&nbsp; method1&nbsp; failed&nbsp; &nbsp; &nbsp; 13&nbsp; method4&nbsp; failed&nbsp; &nbsp; &nbsp; 1The method method1 is defined.Iterating over the table with the params: ('success', 0)Table:&nbsp;&nbsp; &nbsp; &nbsp; name&nbsp; &nbsp; group failed1&nbsp; method2&nbsp; success&nbsp; &nbsp; &nbsp; 02&nbsp; method3&nbsp; success&nbsp; &nbsp; &nbsp; 0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python