How to convert list of model objects to pandas dataframe?

后端 未结 5 1944
遥遥无期
遥遥无期 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.

    0 讨论(0)
  • 2020-12-23 13:54

    try:

    variables = list(array[0].keys())
    dataframe = pandas.DataFrame([[getattr(i,j) for j in variables] for i in array], columns = variables)
    
    0 讨论(0)
  • 2020-12-23 13:56

    Code that leads to desired result:

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

    Thanks to @Serbitar for pointing me to the right direction.

    0 讨论(0)
  • 2020-12-23 14:01

    A much cleaner way to to this is to define a to_dict method on your class and then use pandas.DataFrame.from_records

    class Signal(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def to_dict(self):
            return {
                'x': self.x,
                'y': self.y,
            }
    

    e.g.

    In [87]: signals = [Signal(3, 9), Signal(4, 16)]
    
    In [88]: pandas.DataFrame.from_records([s.to_dict() for s in signals])
    Out[88]:
       x   y
    0  3   9
    1  4  16
    
    0 讨论(0)
  • 2020-12-23 14:06

    Just use:

    DataFrame([o.__dict__ for o in my_objs])
    

    Full example:

    import pandas as pd
    
    # define some class
    class SomeThing:
        def __init__(self, x, y):
            self.x, self.y = x, y
    
    # make an array of the class objects
    things = [SomeThing(1,2), SomeThing(3,4), SomeThing(4,5)]
    
    # fill dataframe with one row per object, one attribute per column
    df = pd.DataFrame([t.__dict__ for t in things ])
    
    print(df)
    

    This prints:

       x  y
    0  1  2
    1  3  4
    2  4  5
    
    0 讨论(0)
提交回复
热议问题