Python类从pandas dataframe中选择功能

问题描述 投票:1回答:1

我正在围绕特定数据集编写一个包装类,该数据集将表示为pandas数据框。大约有15列。我希望用户能够在课程构建时选择他们想要的列。每列都是通过调用类中的方法构造的,即def calculate_feature1():。我正在考虑为每列创建一个布尔构造函数参数,但我想知道是否有更好的模式。

class MyCoolDFWrapper:

    def __init__(include_feature_1=True, include_feature_2=True, etc ...)

    def calc_feature_1():
        pass

    ...

    def calc_feature_n():
        pass

    def get_data(self):
        return self.df[ " go calculate list of features they wanted to include " ]

这可以改善吗?

python pandas
1个回答
2
投票

你可以使用像这样的纯关键字参数:

def my_function(arg1, **kwargs)

传递给my_function的任何参数都将放在字典中。您可以在运行时查看函数中字典的内容。

因此,您可以为它提供所需的15个布尔参数,或者只是包含所需列的整数数组,并基于您可以调用函数的列。但问题是,你真的需要15个函数来计算这些特征吗?

© www.soinside.com 2019 - 2024. All rights reserved.