1 回答
TA贡献1862条经验 获得超7个赞
好的,您的代码的问题是您如何迭代 group by 的结果。
这个答案显示了如何正确迭代。
此代码显示了如何完成您的任务:
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.")
输出:
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
添加回答
举报