"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

November 26, 2016

Day #42 - Classes in python

Today it's bit more on classes in python. It is similar to C# / C++ / Java


import pandas as pd
import random
class DemoClass:
#Class members
df = pd.DataFrame()
a = []
#Initialization
def __init__(self):
self.df = pd.DataFrame({'A':[1,2,3,4],'B':[5,6,7,8]})
for i in range(1,56):
self.a.append(i)
print('Initialized values')
print(self.df)
print(self.a)
#Custom methods modify
def modify(self):
self.df = self.df.drop('A',1)
print('Modified df')
print(self.df)
#Custom methods reinitialize
def reinitialize(self):
print('Reinitialized')
self.df = pd.DataFrame({1:[1,2,3,4],2:[1,2,3,4],10:[1,2,3,4]})
self.df = self.df.drop(1,1)
#Custom methods displaydata
def displaydata(self):
print('In Display Data')
print(random.sample(self.a,15))
print(self.df)
#Object Creation
x = DemoClass()
#Methods Invocation
x.displaydata()
x.modify()
x.displaydata()
x.reinitialize()
x.displaydata()
view raw classexample.py hosted with ❤ by GitHub

No comments: