我目前正在使用Spyder IDE。我正在尝试将基于不同组的自编译函数 sfill 应用于我的数据框。每当我创建此函数时,我都会收到以下错误:
TypeError:sfill()只取1个参数(给定2个)
我确定 sfill() 功能在独立运行时可以正常工作。我究竟做错了什么? 如何在不使用变量资源管理器中的语句 全局需要 的情况下,允许Python显示数据框 想要 ?如何在此瞬间使用return?
我的代码如下:
import pandas as pd
have = pd.DataFrame({ \
"groups": pd.Series(["group1","group1","group1","group2","group2","group2"]) \
,"a0": pd.Series(["abc","1","something here","abc2","1","something here"]) \
,"a1": pd.Series(["","2","something here","","","something here"]) \
,"a2": pd.Series(["","3","something here","","3","something here"]) \
,"a3": pd.Series(["something","1","something here","something","1","something here"]) \
,"a4": pd.Series(["","2","something here","","2","something here"]) \
,"a5": pd.Series(["","","something here","","","something here"]) \
,"a6": pd.Series(["","","something here","","","something here"]) \
,"a7": pd.Series(["cdf","5","something here","mnop","5","something here"]) \
,"a8": pd.Series(["","6","something here","","6","something here"]) \
,"a9": pd.Series(["xyz","1","something here","xyz","1","something here"]) \
})
class main(object):
def sfill(vector):
vector = vector.copy()
vector.i0, vector.i1 = vector.index[0], vector.index[1]
vector.cond = have.loc[vector.i1].ne('')
vector.loc[vector.i0, vector.cond] = vector.loc[vector.i0, vector.cond].str.strip().replace('', None)
return vector
def Development_plan(selfdev):
global want
want=have.groupby('groups', group_keys=False, sort=False).apply(selfdev.sfill)
a=main()
a.Development_plan() # this is where the issue exists
感谢。
答案 0 :(得分:3)
类中的函数应该以{{1}}作为第一个参数
self
当您致电def sfill(self, vector):
...
时,它实际上会调用selfdev.sfill(soemthing)
sfill(selfdev, something)
只是一个命名约定,在您的Development_plan()中使用selfdev。