How to fix 'pandas.core.common' has no attribute 'AbstractMethodError'?

前端 未结 1 676
北荒
北荒 2021-01-19 15:51

I wanted to see the methods available for a pandas object. When I ran this code I got an AttributeError error. I\'ve searched but not found an example of this error or how t

相关标签:
1条回答
  • 2021-01-19 16:55

    Okay the problem of your code is how u are iterating the result of group by.

    This answer shows how to iterate properly.

    This code shows how to do your task:

    import pandas as pd
    
    def method1():
        return 0
    
    exampleDf = 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:
        print("Iterating over the table with the params: " + str(params) )
        print("Table: \n" + str(table))
    
        for index, row in table.iterrows():
            if(row['name'] in dir()):
                print("The method " + str(row['name']) + " is defined.")
    

    Output:

    Iterating over the table with the params: ('failed', 1)
    Table: 
          name   group failed
    0  method1  failed      1
    3  method4  failed      1
    The method method1 is defined.
    Iterating over the table with the params: ('success', 0)
    Table: 
          name    group failed
    1  method2  success      0
    2  method3  success      0
    
    0 讨论(0)
提交回复
热议问题