如何使用具有kwargs的导入函数?

时间:2019-05-14 14:36:21

标签: python import kwargs

我编写了一个函数,该函数基于可变数量的kwargs来子集我的数据框。我正在尝试在Final.py中运行此代码,该代码调用Subsetts.py:

table_2=pd.read_csv('Table_2.csv')
os.chdir(Directory.cwd) 

import Subsetts 
table_2=table_2
sub=Subsetts.Subsetting()
print (table_2.head())
sub.SubsetVariable(table_2, Temperature=180, W=10.0, Curve='Transfer lin')

其中SubsetVariable函数为:

 class Subsetting:     
    def __init__(self):
        pass 


    def SubsetVariable ( self, data, *arg, **kwargs):  # https://stackoverflow.com/questions/22005911/convert-columns-to-string-in-pandas
        '''       
        takes n argumments, joins them in a query, TRANFROMS DATASET TO STR , and gives back subset '''

        self.data=data
        qry = ' & '.join(["{} == '{}'".format(key,value) for key,value in kwargs.items()])        
        df=self.data.astype(str)
        subset=df.query(qry)
        self.subset=subset

    def SubsetUnique(self, data):
        data = data.drop_duplicates(subset='Names', keep='first')

但是它给了我这个错误:

import Subsetts 
table_2=table_2

sub=Subsetts.Subsetting()
print (table_2)
sub.SubsetVariable(data=table_2, Temperature=180, W=10.0, Curve='Transfer lin')
Traceback (most recent call last):

  File "<ipython-input-42-90a4f72190ef>", line 1, in <module>
    import Subsetts

  File "C:\Users\leo\Desktop\Plot_program\SCTFT\Subsetts.py", line 37, in <module>
    sub.SubsetVariable(table_2, Temperature=180, W=10.0, Curve='Transfer lin')

NameError: name 'table_2' is not defined

我刚刚在上面定义了它,所以我不明白为什么它不起作用。我的猜测是,它与** kwargs有关。 当我直接在Subsetts.py中运行该函数时,它可以正常工作。

0 个答案:

没有答案