Home > Software design >  convert a pandas dataframe subclass into a dataframe object?
convert a pandas dataframe subclass into a dataframe object?

Time:01-06

Can i ask, I'm using someone's (private) code and they have this:

class DNATable():
    '''
    A subclass of a Pandas DataFrame, designed to amalgamate data
    easier.
    '''

And so the output is this:

print(test)
<dnatable.dnatable.DNATable object at 0x7f7162e87fd0>

I've tried many ways of converting this to a dataframe, or just doing anything with it, e.g.

print(type(pd.DataFrame(test)))

Returns:

ValueError: DataFrame constructor not properly called!

I also tried returning test.keys() to understand if i could make the subclass into a dict, np.load(test) to see if I could return arrays and str(test) to see it as a string, all of which return similar errors to above saying basically it's not that type of data.

Does someone know how to take an object that's a subclass of a pandas dataframe, and just treat it as a dataframe? (e.g. something like df = pd.DataFrame(test))? Or is it specific to this person's code?

CodePudding user response:

Try DNATable.__dict__

Suppose you have this class

class DNATable():
    def __init__(self, x, y, z):
        self.x = x
        self._y = y
        self.__z = z

l = [DNATable(1, 2, 3), DNATable(4, 5, 6)]

Create the dataframe

df = pd.DataFrame([i.__dict__ for i in l])
print(df)

# Output
   x  _y  _DNATable__z
0  1   2             3
1  4   5             6
  •  Tags:  
  • Related