I'm trying to create a class which inherits a pandas DataFrame, with some modifications. However, it did not work as expected.
import pandas as pd
class result(pd.DataFrame):
def __init__(self, x):
pd.DataFrame.__init__(self)
j = pd.DataFrame({'a': x})
print(x)
print(j)
self.append(j)
The result:
>>> k = result([2,4])
[2, 4]
a
0 2
1 4
>>> print(k)
Empty result
Columns: []
Index: []
As you can see, somehow the returned value is not appended by j
. For comparison, observe when j
and k
are not defined within the class:
>>> k = pd.DataFrame()
>>> j = pd.DataFrame({'a': [2,4]})
>>> print(k.append(j))
a
0 2
1 4
What causes the difference? What should I write within result
if I want the arguments x
to be appended into j
?
Many thanks in advance!