I would like to add rows to a global dataframe from within a different class instances. Can I somehow give the global dataframe as argument when creating the class instance. In the code below the dataframe is a local copy while I would like to change the global df.
I could give the global dataframe as argument in the add_row function directly and it would work but I would like to avoid that.
I'm not sure if this is the right way to do it anyway. My goal is that I can change the same dataframe from within different classes.
import pandas as pd
history_1 = pd.DataFrame()
class ClassA:
def __init__(self, history):
self.history = history
def add_row(self, row):
self.history = pd.concat([self.history, pd.DataFrame([row])])
class ClassB:
def __init__(self, history):
self.history = history
def add_row(self, row):
self.history = pd.concat([self.history, pd.DataFrame([row])])
class_a = ClassA(history_1)
new_row = {'r1':1, 'r2':2, 'r3':3}
class_a.add_row(new_row)
class_b = ClassB(history_1)
new_row = {'r1':1, 'r2':2, 'r3':3}
class_b.add_row(new_row)