Home > Blockchain >  dataframe none inside static function
dataframe none inside static function

Time:01-29

so I'm new to python and was facing an issue with initializing a dataframe inside a static method. Here is my code:

class class1:
     df = pd.DataFrame()

     @staticmethod
     def static_method_1(df):
          class1.df = returns_a_df()
          print(class1.df)

     @staticmethod
     def static_method_2():
          print(class1.df)               // Here, I get it as none

I'm calling it using:

class1.static_method_1(data)               // data is not empty here
class1.static_method_2()    

         // I get here that df is empty     

Since, i'm initializing class1.df in static_method_1, why is it showing empty in static_method_2?I'm able to print it in static_method_1 but not in the other function Any help is appreciated.

  • returns_a_df(): will basically return a df with values
  • I'm able to print in static_method_1 but not in static_method_2
  • How can I initialise it in such a way that it is available to all static methods
  • I'm not using using self here.

CodePudding user response:

You can use class variables and functions to achieve this:

class class1:
 
 def __init__(self):
      self.df = pd.DataFrame()

 def static_method_1(self,df):
      self.df = returns_a_df()
      print(self.df)

 def static_method_2(self):
      print(self.df) 

And call the class using:

w = class1()

or remove the returns_a_df in init and call it seperately:

data = returns_a_df()
w = class1(data)
  •  Tags:  
  • Related