Convert list of dictionaries to a pandas DataFrame

前端 未结 7 2342
清酒与你
清酒与你 2020-11-22 01:22

I have a list of dictionaries like this:

[{\'points\': 50, \'time\': \'5:00\', \'year\': 2010}, 
{\'points\': 25, \'time\': \'6:00\', \'month\': \"february\"         


        
7条回答
  •  一个人的身影
    2020-11-22 01:29

    You can also use pd.DataFrame.from_dict(d) as :

    In [8]: d = [{'points': 50, 'time': '5:00', 'year': 2010}, 
       ...: {'points': 25, 'time': '6:00', 'month': "february"}, 
       ...: {'points':90, 'time': '9:00', 'month': 'january'}, 
       ...: {'points_h1':20, 'month': 'june'}]
    
    In [12]: pd.DataFrame.from_dict(d)
    Out[12]: 
          month  points  points_h1  time    year
    0       NaN    50.0        NaN  5:00  2010.0
    1  february    25.0        NaN  6:00     NaN
    2   january    90.0        NaN  9:00     NaN
    3      june     NaN       20.0   NaN     NaN
    

提交回复
热议问题