I'm trying to create my own class of Dataframe. I would like it has some specific columns when I call it. So I do this:
from pandas import DataFrame
class MyClass(DataFrame):
def __init__(self):
super(MyClass, self).__init__(columns=['Class','Conditions'])
However, when I try to append data to it,
test = MyClass()
#test = MyClass(columns=['Class','Conditions'])
test.loc[2] = ['class2',[1, 2, 3]]
I get an error:
File "C:\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 189, in setitem self._setitem_with_indexer(indexer, value)
File "C:\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 451, in _setitem_with_indexer self.obj._data = self.obj.append(value)._data
File "C:\Miniconda3\lib\site-packages\pandas\core\frame.py", line 6211, in append sort=sort)
File "C:\Miniconda3\lib\site-packages\pandas\core\reshape\concat.py", line 226, in concat return op.get_result()
File "C:\Miniconda3\lib\site-packages\pandas\core\reshape\concat.py", line 428, in get_result return (cons._from_axes(new_data, self.new_axes)
File "C:\Miniconda3\lib\site-packages\pandas\core\generic.py", line 356, in _from_axes return cls(data, **kwargs)
TypeError: init() takes 1 positional argument but 2 were given
I realised if I don't declare the columns in __init__ and I assign them after the class is created, I have no problem.
However, I would like to class be created with those names as columns.
Thank very much.
Condition('var2',op.ne,3)?DataFrame- unless you're going to be adding/ overriding methods etc... It kind of looks like you could just doMyDataFrame = functools.partial(DataFrame, columns=['Class', 'Condtions'])to fix the column argument for the call and use that?