'how to create a dataframe class in pandas/python
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.
Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Christian Karcher |
