I am trying to use class and objects in Python. I want to use process a Dataframe through a class and then make some changes such as changing the datetime time format and removing bad columns. My question is that while doing such modification, I needed to create a variable named req_cols
. Should I also use self.req_cols
while using classes and objects. I won't be using such variable through any of the instance method for sure. When to use self and when not ?
import pandas as pd
class MyClass:
def __init__(self, my_dataframe):
self.dataframe = my_dataframe
def modification(self):
self.dataframe['Date_time'] = self.dataframe['Date'] + ' ' + self.dataframe['Time']
self.dataframe['Date_time'] = pd.to_datetime(self.dataframe['Date_time'],format='%Y-%m-%d %H:%M:%S')
req_cols = [x for x in self.dataframe.columns if 'Unnamed' not in x]
self.dataframe = self.dataframe[req_cols]
return self.dataframe
bn_futures = pd.read_csv('C:\\IData\\RELIANCE-I.txt')
a = MyClass(bn_futures)
# b = MyClass(my_dataframe)