小编aga*_*arg的帖子

如何从类方法将 **kwargs 传递给函数

我需要为机器学习管道创建一个自定义转换器类。它testfun实际上是一个通过 rpy2 访问的 R 函数。testfun然后在test类中使用。我想公开由 表示的 R 函数的所有参数testfun,因此**kwargs。但我不知道如何通过**kwargs。下面的代码会引发错误。

def testfun(x=1, a=1, b=1, c=1):
    return x**a, b**c

class test(BaseEstimator, TransformerMixin):
    def __init__(self, **kwargs):
        self.__dict__.update(**kwargs)
    def testkwargs(self):
        return testfun(**kwargs)
temp = test(x=1,a=1,b=2,c=3)
temp.testkwargs()

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-131-de41ca28c280> in <module>
      5         return testfun(**kwargs)
      6 temp = test(x=1,a=1,b=2,c=3)
----> 7 temp.testkwargs()

<ipython-input-131-de41ca28c280> in testkwargs(self)
      3         self.__dict__.update(**kwargs)
      4     def testkwargs(self):
----> 5         return testfun(**kwargs)
      6 temp = test(x=1,a=1,b=2,c=3)
      7 …
Run Code Online (Sandbox Code Playgroud)

python keyword-argument

6
推荐指数
1
解决办法
7675
查看次数

python自定义汇总表

我有一张桌子,看起来像下面的桌子:

    A   B   C   D
    1   1   2   3
    1   1   3   3
    2   3   0   1
    2   4   2   3
    3   1   4   1
    3   0   2   4
Run Code Online (Sandbox Code Playgroud)

我需要生成一个类似于以下内容的表:

    A   Metric  Min Mean    Max
    1   B       1   1.0     1
        C       2   2.5     3
        D       3   3.0     3
    2   B       3   3.5     4
        C       0   1.0     2
        D       1   2.0     3
    3   B       0   0.5     1
        C       2   3.0     4
        D       1   2.5     4
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经提出了这个建议。但是效果并不理想。我敢肯定有更好的方法:

grouped = df.groupby(['A'])
for name, …
Run Code Online (Sandbox Code Playgroud)

python pandas

1
推荐指数
1
解决办法
53
查看次数

标签 统计

python ×2

keyword-argument ×1

pandas ×1