This code defines a function that extracts all an objects properties into a pandas dataframe. It is created as a row so that each column can have the appropriate datatype. This function is part of great2.
import pandas as pddef rinfo(ob):""" generically export all reasonable data from ob store in DataFrame as a ROW and set reasonable data types accesses non-callable items only :param ob: :param index_attribute: :param timestamp: add a timestamp column """ d = {}for i indir(ob):if i[0] !='_':try: a =getattr(ob, i, None)if a isnotNoneandnotcallable(a): d[i] = [a]exceptExceptionas e: d[i] ='ERROR:'+str(type(e)) index =str(type(ob)) df = pd.DataFrame(d, index=[index]) df.index.name ='id'return df# make a dummy objectx =type('myClass', (), {})x.a =10x.b ='A string'x.c =float.as_integer_ratiox.d =range(5)print(rinfo(x).T)
id <class 'type'>
a 10
b A string
d (0, 1, 2, 3, 4)