Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python class attributes to pandas dataframe

Tags:

python

pandas

I would like to populate a pandas dataframe from attributes of a list of classes generated via 'append'. (not sure this is the right term to describe 'allFoo' below Here is a stripped down example code:

class foo(object):
def __init__(self,input):
    self.val=input
    #in real life, there will be many more attributes in this class
allFoo=[];
for i in range(10):
    allFoo.append(foo(i))

now I would like to define a new pandas data frame 'df' that gets populated from allFoo.val (and not any other attributes)

something like this:

df[0]=foo[0].val
df[1]=foo[1].val

etc

I am coming from matlab where I would try something like this: dataFrame=allFoo[:].val

how can I achieve this in python/pandas?

like image 458
jlarsch Avatar asked Dec 03 '15 11:12

jlarsch


1 Answers

For your "stripped-down" example the following code would do the job:

pd.DataFrame([f.val for f in allFoo], columns=['val'])

In a slightly more general case, where you are sure you can take all field values from your objects, the following should work just as well:

pd.DataFrame([vars(f) for f in allFoo])

In a yet more general case, when your objects may contain some fields that you need in the data frame, and other fields which you do not, there is no way around specifying this list of fields. The following code might help then:

fields = ['val', 'other_field']
pd.DataFrame([{fn: getattr(f, fn) for fn in fields} for f in allFoo])

The moral: whenever you do not know a "built-in method" for something, list comprehension is your first choice.

like image 109
KT. Avatar answered Oct 12 '22 12:10

KT.