title: Make a Class(updating) using pandas data frame
body: <p>I am trying to make a simple class which would store data as a dataframe and give a certain result. I have tried to write one as below:</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  
  class logdata():
      def __init__(self,size):
          self.size = size
          self.x = None
          self.timestamp = None
          self.confidence = 100
          self.occurance = 1
  
  
      def createdf(self):
          self.df = pd.DataFrame(data = None, columns = ['Timestamp','Label','Occurance', 'Confidence'])
          return self.df
  
  
      def insertdf(self, x, timestamp):
          self.occurance = self.get_occurance()
          self.confidence = self.get_confidence()
          self.df.loc[-1] = pd.Series({'Timestamp':timestamp, 'Label': x, 'Occurance':self.occurance, 'Confidence':self.confidence})
          self.df.index = self.df.index + 1
          self.df = self.df.sort_index()
          self.df = self.del_row()
          return self.df
  
      def get_occurance(self):
          return self.df.loc[self.df.Label == self.x, 'Label'].count()
  
      def get_confidence(self):
          y = self.df.shape[0]
          if y:
              conf = (self.occurance/self.df.shape[0])*100
          else:
              conf = 100
          return conf
  
      def del_row(self):
          if self.df.shape[0] &gt; int(self.size):
              self.df = self.df.iloc[self.size:]
          return self.df
  
      def get_result(self):
          return self.df.loc[self.createdf['Confidence'].idxmax()]
  </code></pre>
  
  <p>What this does is when I pass a data such as integer it will create a new empty dataframe if there is none present and store it in the first line by calling say <code>ld = logdata()</code>{I can also set max size as <code>ld.size = 10</code>} followed by <code>ld.createdf()</code>, then I would insert the first data into the dataframe by calling <code>ld.insertdf(x,timestamp)</code> which would compute the occurance(default = 1) and confidence(mean as percentage, default = 100) by the following functions. Finally, I want to extract the data which has the highest confidence by calling <code>ld.getresult()</code> which I would like to send to a server(I know this part) using pymongo.</p>
  
  <p>I am not much of a data structures guy, just a noob in python. I searched for a lot of tutorials but ended up with getting tut for subclass of dataframes. This doesnt seem to work, if possible please help me with the mistakes. You are free to criticise this constructively. It'll help me a lot, thanks.</p>
  
  <p>Here's an example:
  Suppose I have a binary Label 1 and 0 with size 3, so i'll first set <code>ld.size = 3</code>
  Then my inputs will be <code>ld.insertdf(0,1500)</code>
  which will create:</p>
  
  <pre><code>Timestamp | Label | Occurance | Confidence 
  |   1500  |     0 |          1|        100
  </code></pre>
  
  <p>Then I add <code>ld.insertdf(0,1530)</code>
  Which updates to:</p>
  
  <pre><code>Timestamp | Label | Occurance | Confidence 
  |   1530  |     0 |          2|        100
  |   1500  |     0 |          2|        100
  </code></pre>
  
  <p>Finally when I add <code>ld.insertdf(1,1600)</code>
  It should update to:</p>
  
  <pre><code>Timestamp | Label | Occurance | Confidence 
  |   1600  |     1 |          1|         33
  |   1530  |     0 |          2|         66
  |   1500  |     0 |          2|         66
  </code></pre>
  
  <p>When I add another <code>ld.insertdf(0,1630)</code>
  It will change the df as:</p>
  
  <pre><code>Timestamp | Label | Occurance | Confidence 
  |   1630  |     0 |          2|         66
  |   1600  |     1 |          1|         33
  |   1530  |     0 |          2|         66
  </code></pre>
  
  <p>as size limit is 3.
  PS - In the comment I reversed the index while explaining, but the method is self explanatory.</p>
  
  <p><code>ld.get_result()</code> will just give me the label with the highest dataset which is also the latest input, i.e: <code>1630,0,2,66</code></p>
  
  <p>Edit: I have editied the code which allows me to create a dataframe but it doesn't update the occurance and confidences.</p>