简体   繁体   English

Python 将不同的变量分配给 class object

[英]Python assign different variables to a class object

This is a general python question.这是一个一般的 python 问题。 Is it possible to assign different variables to a class object and then perform different set of operations on those variables?是否可以将不同的变量分配给 class object,然后对这些变量执行不同的操作集? I'm trying to reduce code but maybe this isn't how it works.我正在尝试减少代码,但也许这不是它的工作方式。 For example, I'm trying to do something like this:例如,我正在尝试做这样的事情:

Edit: here is an abstract of the class and methods:编辑:这是 class 和方法的摘要:

class Class:
    def __init__(self, df):
        self.df = df

    def query(self, query):
        self.df = self.df.query(query)
        return self

    def fill(self, filter):
        self.df.update(df.filter(like=filter).mask(lambda x: x == 0).ffill(1))
        return self

    def diff(self, cols=None, axis=1):
        diff = self.df[self.df.columns[~self.df.columns.isin(cols)]].diff(axis=axis)
        self.df = diff.join(self.df[self.df.columns.difference(diff.columns)])
        return self

    def melt(self, cols, var=None, value=None):
        return pd.melt(self.df, id_vars=columns, var_name=var, value_name=value)

I'm trying to use it like this:我正在尝试像这样使用它:

df = pd.read_csv('data.csv')

df = Class(df)
df = df.query(query).forward_fill(include)

df_1 = df.diff(cols).melt(cols)

df_2 = df.melt(cols)

df_1 and df_2 should have different values, however they are the same as df_1 . df_1df_2应该有不同的值,但是它们与df_1相同。 This issue is resolved if I use the class like this:如果我像这样使用 class,这个问题就解决了:

df_1 = pd.read_csv('data.csv')
df_2 = pd.read_csv('data.csv')

df_1 = Class(df_1)
df_2 = Class(df_2)

df_1 = df_1.query(query).forward_fill(include)
df_2 = df_2.query(query).forward_fill(include)

df_1 = df_1.diff(cols).melt(cols)

df_2 = df_2.melt(cols)

This results in extra code.这会导致额外的代码。 Is there a better way to do this where you can use an object differently on different variables, or do I have to create seperate objects if I'm trying to have two variables perform separate operations and return different values?有没有更好的方法可以做到这一点,您可以在不同的变量上以不同的方式使用 object,或者如果我试图让两个变量执行单独的操作并返回不同的值,我是否必须创建单独的对象?

With the return self statement in the diff - method you return the reference of the object.使用diff - 方法中的return self语句返回 object 的引用。 The same thing happens after the melt method. melt方法后也会发生同样的事情。 But in that two methods you allreadey manipulated the origin df .但是在这两种方法中,您已经操纵了原点df

Here:这里:

1 df = pd.read_csv('data.csv')
2
3 df = Class(df)
4 df = df.query(query).forward_fill(include)
5 
6 df_1 = df.diff(cols).melt(cols)

the df has the same values like df_1 . df具有与df_1相同的值。 I guess the melt method without other args then cols arguments only assigns col names or something like that.我猜没有其他参数的melt方法然后 cols arguments 只分配 col 名称或类似的东西。 Subsequently df_2=df.melt(cols) would have the same result like df_2=df_1.melt(cols) .随后df_2=df.melt(cols)将具有与df_2=df_1.melt(cols)相同的结果。

If you want to work with one object, you dont should use self.df=... in your class methods, because this changes the instance value of df .如果您想使用一个 object,则不应在 class 方法中使用self.df=... ,因为这会更改df的实例值。 You only need to write df =... and than return Class(df) .您只需要编写df =...而不是 return Class(df)

For example:例如:

def diff(self, cols=None, axis=1):
    diff = self.df[self.df.columns[~self.df.columns.isin(cols)]].diff(axis=axis)
    df = diff.join(self.df[self.df.columns.difference(diff.columns)])
    return Class(df)

Best regards此致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM