So, I really want to add on a few new methods to pandas.core.frame.DataFrame
s. For example, I want a method called .idx
:
pandas.core.frame.DataFrame.idx(self, rows, cols):
return self.iloc[rows].loc[cols]
Is this possible? I'm not sure what the syntax should be - the class already exists from a library, so I can't just put the function inside the class definition.
Could you not extend your DataFrame
, inheriting all of its functions, and having the freedom to then define your own, on top?
In python it would look something like:
class ExtendedDataFrame(DataFrame):
...
Then, simply use an instance of ExtendedDataFrame
, instead of DataFrame
, for your extra goodies.
I also suggest taking a look at this tutorial covering inheritance.
Also be sure to check out your version of python's implementation of super()
, if you're trying to override functions of the super (parent) class.
Here is a SO question with great information regarding super()
, because I'm noticing the tutorial I linked only briefly covers super()
, and in the comments no less.
This is actually really easy in Python. While I would recommend inheriting from DataFrame, as MeetTitan answered, sometimes that won't work, but you can just add on functions like this:
class Abc(object):
pass
def new_funct(self):
print 1234
Abc.instance_funct = new_funct
a = Abc()
a.instance_funct()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.