Pandas data frame to dictionary of lists

前端 未结 2 897
情歌与酒
情歌与酒 2020-12-11 02:32

How to use python or pandas (preferably) to convert a pandas data_frame to dictionary of lists for input into highcharts.

The closest I got to was (see tmp notebook

相关标签:
2条回答
  • 2020-12-11 02:45
    In [199]: df2.reset_index().to_dict(orient='list')
    Out[199]: 
    {'date': ['2014-10-1', '2014-10-2', '2014-10-3', '2014-10-4', '2014-10-5'],
     'foo': [8, 1, 8, 8, 1],
     'temp': [10, 10, 8, 3, 10],
     'time': [1, 2, 3, 4, 5]}
    
    0 讨论(0)
  • 2020-12-11 02:53

    to create a list of the dictionaries per line

    post_data_list = []
    for i in df2.index:
      data_dict = {}
      for column in df2.columns:
        data_dict[column] = df2[column][i]
      post_data_list.append(data_dict)
    
    0 讨论(0)
提交回复
热议问题