Convert Python dict into a dataframe

前端 未结 16 2393
暗喜
暗喜 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: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 ...

提交回复
热议问题