将数据存储在类中,并动态访问数据作为类属性

发布于 2025-02-06 18:11:04 字数 1509 浏览 3 评论 0 原文

我正在尝试编写一个类,该类将数据框架ID作为字符串和值作为数据框的值,并创建访问数据的类属性。

我能够编写一个类似类的小例子,该类别需要以静态方式创建的方法并将对象返回为类方法,但我想循环浏览数据,以 df的键键入键 s,并允许使用属性访问每个 df

最低工作示例

from dataclasses import dataclass
import pandas as pd

# re-writing as dataclass
@dataclass
class Dataset:

    # data container dictionary as class attribute
    dict = {'df1_id':pd.DataFrame({'col1':[1,1]}), 
            'df2_id':pd.DataFrame({'col2':[2,2]}), 
            'df3_id':pd.DataFrame({'col3':[3,3]})}
    
    
    def df1_id(self) -> pd.DataFrame:# class method to create as class attribute
        return dict['df1_id']

    def df2_id(self) -> pd.DataFrame:# same class method above
        return dict['df2_id']

    def df3_id(self) -> pd.DataFrame:# same class method above
        return dict['df3_id']

    def dataframes_as_class_attributes(self):
        # store the dfs to access as class attributes
        # replacing 3 methods above
        return 

结果

datasets = Dataset()

print(datasets.df1_id())

预期结果

datasets = Dataset()

print(datasets.df1_id) # class attribute created by looping through the dict object

编辑:

I am trying to write a class that takes data where the dataframe IDs as strings and the values as DataFrames and create class attributes accessing the data.

I was able to write a small example of a similar class that needs the methods to be created in a static manner and return the objects as class methods but I would like to loop over the data, taking in the keys for the dfs and allow for access to each df using attributes.

minimum working example

from dataclasses import dataclass
import pandas as pd

# re-writing as dataclass
@dataclass
class Dataset:

    # data container dictionary as class attribute
    dict = {'df1_id':pd.DataFrame({'col1':[1,1]}), 
            'df2_id':pd.DataFrame({'col2':[2,2]}), 
            'df3_id':pd.DataFrame({'col3':[3,3]})}
    
    
    def df1_id(self) -> pd.DataFrame:# class method to create as class attribute
        return dict['df1_id']

    def df2_id(self) -> pd.DataFrame:# same class method above
        return dict['df2_id']

    def df3_id(self) -> pd.DataFrame:# same class method above
        return dict['df3_id']

    def dataframes_as_class_attributes(self):
        # store the dfs to access as class attributes
        # replacing 3 methods above
        return 

result

datasets = Dataset()

print(datasets.df1_id())

expected result

datasets = Dataset()

print(datasets.df1_id) # class attribute created by looping through the dict object

Edit:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2025-02-13 18:11:05

摄入 df s的键,并允许使用属性访问每个 df

似乎班级的唯一目的是具有属性访问语法。在这种情况下,只需创建a >对象。

from types import SimpleNamespace

class Dataset(SimpleNamespace):
    pass 
    # extend it possibly 

data = {
    'df1_id':pd.DataFrame({'col1':[1,1]}), 
    'df2_id':pd.DataFrame({'col2':[2,2]}), 
    'df3_id':pd.DataFrame({'col3':[3,3]})
}

datasets = Dataset(**data)

输出:

>>> datasets.df1_id 

   col1
0     1
1     1

>>> datasets.df2_id 

   col2
0     2
1     2

>>> datasets.df3_id 

   col3
0     3
1     3

taking in the keys for the dfs and allow for access to each df using attributes.

It seems that the only purpose of the class is to have attribute access syntax. In that case, it would be simpler to just create a namespace object.

from types import SimpleNamespace

class Dataset(SimpleNamespace):
    pass 
    # extend it possibly 

data = {
    'df1_id':pd.DataFrame({'col1':[1,1]}), 
    'df2_id':pd.DataFrame({'col2':[2,2]}), 
    'df3_id':pd.DataFrame({'col3':[3,3]})
}

datasets = Dataset(**data)

Output:

>>> datasets.df1_id 

   col1
0     1
1     1

>>> datasets.df2_id 

   col2
0     2
1     2

>>> datasets.df3_id 

   col3
0     3
1     3
失眠症患者 2025-02-13 18:11:04

您可以使用 setAttr 如下所示:

from dataclasses import dataclass
import pandas as pd

@dataclass
class Dataset:

    dict_ = {'df1_id':pd.DataFrame({'col1':[1,1]}), 
            'df2_id':pd.DataFrame({'col2':[2,2]}), 
            'df3_id':pd.DataFrame({'col3':[3,3]})}
            
    def __post_init__(self):
        for key, val in self.dict_.items():
            setattr(self, key, val)

为了避免与Python关键字发生冲突,将单个尾随下划线放置在变量名称之后。 ( pep 8

You could use setattr like below:

from dataclasses import dataclass
import pandas as pd

@dataclass
class Dataset:

    dict_ = {'df1_id':pd.DataFrame({'col1':[1,1]}), 
            'df2_id':pd.DataFrame({'col2':[2,2]}), 
            'df3_id':pd.DataFrame({'col3':[3,3]})}
            
    def __post_init__(self):
        for key, val in self.dict_.items():
            setattr(self, key, val)

To avoid conflicts with python keywords put a single trailing underscore after variable name. (PEP 8)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文