Convert Python dict into a dataframe

前端 未结 16 2375
暗喜
暗喜 2020-11-22 03:18

I have a Python dictionary like the following:

{u\'2012-06-08\': 388,
 u\'2012-06-09\': 388,
 u\'2012-06-10\': 388,
 u\'2012-06-11\': 389,
 u\'2012-06-12\':          


        
相关标签:
16条回答
  • 2020-11-22 04:06

    This is what worked for me, since I wanted to have a separate index column

    df = pd.DataFrame.from_dict(some_dict, orient="index").reset_index()
    df.columns = ['A', 'B']
    
    0 讨论(0)
  • 2020-11-22 04:07

    Pass the items of the dictionary to the DataFrame constructor, and give the column names. After that parse the Date column to get Timestamp values.

    Note the difference between python 2.x and 3.x:

    In python 2.x:

    df = pd.DataFrame(data.items(), columns=['Date', 'DateValue'])
    df['Date'] = pd.to_datetime(df['Date'])
    

    In Python 3.x: (requiring an additional 'list')

    df = pd.DataFrame(list(data.items()), columns=['Date', 'DateValue'])
    df['Date'] = pd.to_datetime(df['Date'])
    
    0 讨论(0)
  • 2020-11-22 04:07
    d = {'Date': list(yourDict.keys()),'Date_Values': list(yourDict.values())}
    df = pandas.DataFrame(data=d)
    

    If you don't encapsulate yourDict.keys() inside of list() , then you will end up with all of your keys and values being placed in every row of every column. Like this:

    Date \ 0 (2012-06-08, 2012-06-09, 2012-06-10, 2012-06-1...
    1 (2012-06-08, 2012-06-09, 2012-06-10, 2012-06-1...
    2 (2012-06-08, 2012-06-09, 2012-06-10, 2012-06-1...
    3 (2012-06-08, 2012-06-09, 2012-06-10, 2012-06-1...
    4 (2012-06-08, 2012-06-09, 2012-06-10, 2012-06-1...

    But by adding list() then the result looks like this:

    Date Date_Values 0 2012-06-08 388 1 2012-06-09 388 2 2012-06-10 388 3 2012-06-11 389 4 2012-06-12 389 ...

    0 讨论(0)
  • 2020-11-22 04:10

    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-02  392
    1   2012-07-06  392
    2   2012-06-29  391
    3   2012-06-28  391
    ...
    
    In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
    Out[12]:
              Date  DateValue
    0   2012-07-02        392
    1   2012-07-06        392
    2   2012-06-29        391
    

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

    In [21]: s = pd.Series(d, name='DateValue')
    Out[21]:
    2012-06-08    388
    2012-06-09    388
    2012-06-10    388
    
    In [22]: s.index.name = 'Date'
    
    In [23]: s.reset_index()
    Out[23]:
              Date  DateValue
    0   2012-06-08        388
    1   2012-06-09        388
    2   2012-06-10        388
    
    0 讨论(0)
提交回复
热议问题