Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (50.2k points)

I have an array of objects of this class

class CancerDataEntity(Model):

    age = columns.Text(primary_key=True)

    gender = columns.Text(primary_key=True)

    cancer = columns.Text(primary_key=True)

    deaths = columns.Integer()

    ...

When printed, the array looks like this

[CancerDataEntity(age=u'80-85+', gender=u'Female', cancer=u'All cancers (C00-97,B21)', deaths=15306), CancerDataEntity(...

I want to convert this to a data frame so I can play with it in a more suitable way to me - to aggregate, count, sum and similar. How I wish this data frame to look, would be something like this:

     age     gender   cancer deaths

0    80-85+  Female   ... 15306

1    ...

Is there a way to achieve this using numpy/pandas easily, without manually processing the input array?

1 Answer

0 votes
by (108k points)

You can refer the following code that leads to the desired result:

variables = arr[0].keys()

df = pd.DataFrame([[getattr(i,k) for k in variables] for i in arr], columns = variables)

Browse Categories

...