Home > Net >  how to create a dataframe class in pandas/python
how to create a dataframe class in pandas/python

Time:04-01

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.

CodePudding user response:

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()
  • Related