Pandas data frame to dictionary of lists

How can I use python or pandas (preferred) to convert pandas data_frame to dictionary of lists for input to highcharts.

The closest I got (see tmp notebook here: https://b9ad8d-iad-002.tmpnb.org/user-fVD4OAy9K8wR/notebooks/pd_to_json.ipynb )

df.T.to_json ('bar.json', orient = 'index') But this is "dictate of dictates" instead of "dictate of lists"

My input:

import pandas
import numpy as np

df = pandas.DataFrame({
    "date": ['2014-10-1', '2014-10-2', '2014-10-3', '2014-10-4', '2014-10-5'],
    "time" : [1,2,3,4,5],
    "temp" : np.random.random_integers(0,10,5),
    "foo" : np.random.random_integers(0,10,5)
})

df2 = df.set_index(['date'])
df2

      

enter image description here

Desired Output: I am using this output in Highcharts, which requires it to be a "dictionary of lists" like this:

{
"date": [
    "2014-10-1",
    "2014-10-2",
    "2014-10-3",
    "2014-10-4",
    "2014-10-5"
],
"foo": [
    7,
    2,
    5,
    5,
    6
],
"temp": [
    8,
    6,
    10,
    10,
    3
],
"time": [
    1,
    2,
    3,
    4,
    5
]

      

}

Any help is greatly appreciated!

+3


source to share


1 answer


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]}

      



+4


source







All Articles