Search code examples
pythonpandasdataframedictionary

Convert Python dict into a dataframe


I have a Python dictionary:

{u'2012-07-01': 391,
 u'2012-07-02': 392,
 u'2012-07-03': 392,
 u'2012-07-04': 392,
 u'2012-07-05': 392,
 u'2012-07-06': 392}

I would like to convert this into a pandas dataframe by having the dates and their corresponding values as two separate columns; the expected result looks like:

     Date         DateValue
0    2012-07-01    391
1    2012-07-02    392
2    2012-07-03    392
.    2012-07-04    392
.    ...           ...

Is there a direct way to do this?


Solution

  • The error here, is since calling the DataFrame constructor with scalar values (where it expects values to be a list/dict/... i.e. have multiple columns):

    pd.DataFrame(d)
    ValueError: If using all scalar values, you must must pass an index
    

    You could take the items from the dictionary (i.e. the key-value pairs):

    In [11]: pd.DataFrame(d.items())  # or list(d.items()) in python 3
    Out[11]:
                0    1
    0  2012-07-01  391
    1  2012-07-02  392
    2  2012-07-03  392
    3  2012-07-04  392
    4  2012-07-05  392
    5  2012-07-06  392
    
    In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
    Out[12]:
             Date  DateValue
    0  2012-07-01        391
    1  2012-07-02        392
    2  2012-07-03        392
    3  2012-07-04        392
    4  2012-07-05        392
    5  2012-07-06        392
    

    But I think it makes more sense to pass the Series constructor:

    In [20]: s = pd.Series(d, name='DateValue')
    
    In [21]: s
    Out[21]:
    2012-07-01    391
    2012-07-02    392
    2012-07-03    392
    2012-07-04    392
    2012-07-05    392
    2012-07-06    392
    Name: DateValue, dtype: int64
    
    In [22]: s.index.name = 'Date'
    
    In [23]: s.reset_index()
    Out[23]:
             Date  DateValue
    0  2012-07-01        391
    1  2012-07-02        392
    2  2012-07-03        392
    3  2012-07-04        392
    4  2012-07-05        392
    5  2012-07-06        392