Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:51:47+00:00 2026-06-17T15:51:47+00:00

In my project I build a class with pandas DataFrame as a core. The

  • 0

In my project I build a class with pandas DataFrame as a core. The values in dataframe depends upon some specification and I initialise it with some letter representing the data I want to work with. I put all my functions to create dataframe inside an __init__ as I understand this functions are one off only and no needs for them after the initialisation. Also I don’t want to have access to this functions after my class is in use in later code. (I am not sure if this is “pythonic” way to do so).

After building basic class with __str__ and plotData() methods I would like to apply some filters and build a new class where additional column is the filter. I would like to do that in __init__ but keep everything what already was done. In another words I don’t want to re-write the whole __init__ only want to add new column to the basic dataframe.

In similar fashion I would like to add an additional plot in the plotData() function

My original code has already quite a few of lines but the principles are very similar to code listed below.

import pandas as pd
import pylab as pl
class myClass(object):
    def __init__(self, frameType = 'All'):
        def method1():
            myFrame = pd.DataFrame({'c1':[1,2,3],'c2':[4,5,6],'c3':[7,8,9]})
            return myFrame
        def method2():
            myFrame = pd.DataFrame({'c1':[.1,.2,.3],'c2':[.4,.5,.6],'c3':[.7,.8,.9]})
            return myFrame
        def makingChiose(self):
            if self.frameType == 'All':
                variable = method1() + method2() 
            elif self.frameType == 'a':
                variable = method1()
            elif self.frameType == 'b':
                variable = method2()
            else:
                variable =  pd.DataFrame({'c1':[0,0,0],'c2':[0,0,0],'c3':[0,0,0]})
            #print 'FROM __init__ : %s' % variable
            return variable           
        self.frameType = frameType      
        self.cObject = makingChiose(self) # object created by the class
    def __str__(self):
        return str(self.cObject)
    def plotData(self):
        self.fig1 = pl.plot(self.cObject['c1'],self.cObject['c2'])
        self.fig2 = pl.plot(self.cObject['c1'],self.cObject['c3'])
        pl.show()

class myClassAv(myClass):
    def addingCol(self):
        print 'CURRENT cObject \n%s' % self.cObject # the object is visible 
        self.cObject['avarage'] = (self.cObject['c1']+self.cObject['c2']+self.cObject['c3'])/3
        print 'THIS WORKS IN GENERAL\n%s' % str((self.cObject['c1']+self.cObject['c2']+self.cObject['c3'])/3) # creating new column works
    def plotData(self):
        # Function to add new plot to already existing plots
        self.fig3 = pl.plot(self.cObject['c1'],self.cObject['avarage'])
if __name__ == '__main__':
    myObject1 = myClass()
    print 'myObject1 =\n%s' % myObject1
    myObject1.plotData()
    myObject2 = myClass('a')
    print 'myObject2 =\n%s' % myObject2
    myObject3 = myClass('b')
    print 'myObject3 =\n%s' % myObject3
    myObject4 = myClass('c')
    print 'myObject4 =\n%s' % myObject4

    myObject5 = myClassAv('a').addingCol()
    print 'myObject5 =\n%s' % myObject5
    myObject5.plotData()

Most of the code works, at least in the initialisation but I have an error when I try to create new dataframe with additional column. When I put as the new __init__ I create a completely new initialisation and I loose all what was already done. I created a new function but I would prefer have the additional column after I call a new class not a function inside the new class The output from the code looks like this:

myObject1 =
    c1   c2   c3
0  1.1  4.4  7.7
1  2.2  5.5  8.8
2  3.3  6.6  9.9
myObject2 =
   c1  c2  c3
0   1   4   7
1   2   5   8
2   3   6   9
myObject3 =
    c1   c2   c3
0  0.1  0.4  0.7
1  0.2  0.5  0.8
2  0.3  0.6  0.9
myObject4 =
   c1  c2  c3
0   0   0   0
1   0   0   0
2   0   0   0
CURRENT cObject 
   c1  c2  c3
0   1   4   7
1   2   5   8
2   3   6   9
THIS WORKS IN GENERAL
0    4
1    5
2    6
myObject5 =
None
Traceback (most recent call last):
  File "C:\Users\src\trys.py", line 57, in <module>
    myObject5.plotData()
AttributeError: 'NoneType' object has no attribute 'plotData'

The question is: Can I ‘partially’ override the superclass’s method to have what was previously inside this method with some new functionality? I would like to initialise myClassAv() to dataframe with four columns instead of three like myClass() and I’d like to have myClassAv().plotData() to plot a third line but keep two from base class.

I don’t know how to interpret an error and why myObject5 is None, but I suspect it is something with inheritance.

Also if you have suggestion that I should do all my idea in different way I will appreciate to hear them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:51:48+00:00Added an answer on June 17, 2026 at 3:51 pm

    How about just call myClass.__init__ inside myClassAv.__init__:

    def __init__(self, frameType='All'):
        myClass.__init__(self, frameType)
        def addingCol(cObject): 
            ...
        addingCol(self.cObject)
    

    For concreteness,

    import pandas as pd
    import pylab as pl
    import numpy as np
    
    
    class myClass(object):
        def __init__(self, frameType='All'):
            def method1():
                myFrame = pd.DataFrame(
                    {'c1': [1, 2, 3], 'c2': [4, 5, 6], 'c3': [7, 8, 9]})
                return myFrame
    
            def method2():
                myFrame = pd.DataFrame(
                    {'c1': [.1, .2, .3], 'c2': [.4, .5, .6], 'c3': [.7, .8, .9]})
                return myFrame
    
            def makingChoice(self):
                if self.frameType == 'All':
                    variable = method1() + method2()
                elif self.frameType == 'a':
                    variable = method1()
                elif self.frameType == 'b':
                    variable = method2()
                else:
                    variable = pd.DataFrame(
                        {'c1': [0, 0, 0], 'c2': [0, 0, 0], 'c3': [0, 0, 0]})
                # print 'FROM __init__ : %s' % variable
                return variable
            self.frameType = frameType
            self.cObject = makingChoice(self)  # object created by the class
    
        def __str__(self):
            return str(self.cObject)
    
        def plotData(self):
            self.fig1 = pl.plot(self.cObject['c1'], self.cObject['c2'])
            self.fig2 = pl.plot(self.cObject['c1'], self.cObject['c3'])
            pl.show()
    
    
    class myClassAv(myClass):
        def __init__(self, frameType='All'):
            myClass.__init__(self, frameType)
    
            def addingCol(cObject):
                print 'CURRENT cObject \n%s' % cObject  # the object is visible
                cObject['average'] = cObject.mean(axis=1)
                # creating new column works
                print 'THIS WORKS IN GENERAL\n%s' % str(cObject['average'])
                return cObject
    
            addingCol(self.cObject)
    
        def plotData(self):
            # Function to add new plot to already existing plots
            self.fig3 = pl.plot(self.cObject['c1'], self.cObject['average'])
    
    if __name__ == '__main__':
        myObject1 = myClass()
        print 'myObject1 =\n%s' % myObject1
        myObject1.plotData()
        myObject2 = myClass('a')
        print 'myObject2 =\n%s' % myObject2
        myObject3 = myClass('b')
        print 'myObject3 =\n%s' % myObject3
        myObject4 = myClass('c')
        print 'myObject4 =\n%s' % myObject4
    
        myObject5 = myClassAv('a')
        print 'myObject5 =\n%s' % myObject5
        myObject5.plotData()
    

    By the way, instead of

    self.cObject['avarage'] = (self.cObject['c1']+self.cObject['c2']+self.cObject['c3'])/3
    

    you can use mean(axis = 1):

    self.cObject['average'] = self.cObject.mean(axis=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a class project to build a little Connect4 game in Java.
the structure of the example project is: . |-- ./build | `-- ./build/TestAntLoadFile.class |--
In our school, it is common to build games as class projects in implementing
Can we set the build settings for different targets in the project build settings
We have several independent builds (each independent build is a multi-project build). The main
I've just completed an automation script that: downloads a project build to local storage
I wish to create a zip archive of my target directory (${project.build.directory). using the
I try to build project in Eclipse on Linux Ubuntu. Eclipse show error message:
I create an Android project for build my custom widget, and I want to
I have two projects.One project is build in MVC asp.net and the other project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.