Convert Python dict into a dataframe

前端 未结 16 2373
暗喜
暗喜 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 03:57

    When converting a dictionary into a pandas dataframe where you want the keys to be the columns of said dataframe and the values to be the row values, you can do simply put brackets around the dictionary like this:

    >>> dict_ = {'key 1': 'value 1', 'key 2': 'value 2', 'key 3': 'value 3'}
    >>> pd.DataFrame([dict_])
    
        key 1     key 2     key 3
    0   value 1   value 2   value 3
    

    It's saved me some headaches so I hope it helps someone out there!

    EDIT: In the pandas docs one option for the data parameter in the DataFrame constructor is a list of dictionaries. Here we're passing a list with one dictionary in it.

提交回复
热议问题