1

I have a class where the only input is a dataframe. The first function within the class adds columns to the dataframe to create a new dataframe. I want subsequent functions within the class to use the new dataframe.

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

    def df_manip(self):
        # add additional columns to the dataframe
        return new_df

    def function1(self):
        answer = new_df.column3.sum()
        return answer 

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

house_1 = House(df)
print(house_1.function1())
1
  • 1
    In df_manip add self.df = new_df - then use self.df throughout. Commented Jun 4, 2020 at 2:27

1 Answer 1

2

Either make a new attribute in df_manip or just call df_manip and use its return value

class House:
    def __init__(self, df):
        self.df = df
        # call to make or change attribute
        self.df_manip()

    def df_manip(self):
        # add additional columns to the dataframe 

        # make or change an attribute 
        #self.otherdf = new_df
        #self.df = new_df

        return new_df

    def function1(self):
        answer = new_df.column3.sum()

        # Use  modified attribute
        #answer = self.df.column3.sum()

        # Use df_manip return value
        #newdf = self.df_manip()
        #answer = newdf.column3.sum()

        return answer 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.