Pandas: A clean way to initialize data frame with a list of namedtuple

后端 未结 2 2072
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 15:23

I\'m new to pandas, therefore perhaps I\'m asking a very stupid question. Normally initialization of data frame in pandas would be column-wise, where I put in dict with key

相关标签:
2条回答
  • 2020-12-14 16:15

    The function you want is from_records.

    For namedtuple instances you must pass the _fields property of the namedtuple to the columns parameter of from_records, in addition to a list of namedtuples:

    df = pd.DataFrame.from_records(
       [namedtuple_instance1, namedtuple_instance2],
       columns=namedtuple_type._fields
    )
    

    If you have dictionaries, you can use it directly as

    df = pd.DataFrame.from_records([dict(a=1, b=2), dict(a=2, b=3)])
    
    0 讨论(0)
  • 2020-12-14 16:20

    In a similar vein to creating a Series from a namedtuple, you can use the _fields attribute:

    In [11]: Point = namedtuple('Point', ['x', 'y'])
    
    In [12]: points = [Point(1, 2), Point(3, 4)]
    
    In [13]: pd.DataFrame(points, columns=Point._fields)
    Out[13]: 
       x  y
    0  1  2
    1  3  4
    

    Assuming they are all of the same type, in this example all Points.

    0 讨论(0)
提交回复
热议问题