How to convert list of model objects to pandas dataframe?

后端 未结 5 1945
遥遥无期
遥遥无期 2020-12-23 13:30

I have an array of objects of this class

class CancerDataEntity(Model):

    age = columns.Text(primary_key=True)
    gender = columns.Text(primary_key=True)         


        
5条回答
  •  一整个雨季
    2020-12-23 13:53

    I would like to emphasize Jim Hunziker's comment.

    pandas.DataFrame([vars(s) for s in signals])
    

    It is far easier to write, less error-prone and you don't have to change the to_dict() function every time you add a new attribute.

    If you want the freedom to choose which attributes to keep, the columns parameter could be used.

    pandas.DataFrame([vars(s) for s in signals], columns=['x', 'y'])
    

    The downside is that it won't work for complex attributes, though that should rarely be the case.

提交回复
热议问题