0

i want to create a class, which will create a dataframe object.

but the below code doesn't work properly:

class Df:

    def __init__(self,df):
        self = df

my_df = Df(df)
print(my_df.columns)

AttributeError: 'Df' object has no attribute 'columns'

what should I do? I want to create custom methods in order to manipulate dataframe, that's why I want to use oop.

1
  • 2
    Thinking about inheritance? Commented Apr 1, 2022 at 9:01

1 Answer 1

3

You could directly inherit from Dataframe and add methods to this class:

import pandas as pd

class Df(pd.DataFrame):

    def say_hello(self):
        print('hello world')

df = Df({'data': [1,2,3]})
print(df.columns)
df.say_hello()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.