Convert Python dict into a dataframe

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

    I have run into this several times and have an example dictionary that I created from a function get_max_Path(), and it returns the sample dictionary:

    {2: 0.3097502930247044, 3: 0.4413177909384636, 4: 0.5197224051562838, 5: 0.5717654946470984, 6: 0.6063959031223476, 7: 0.6365209824708223, 8: 0.655918861281035, 9: 0.680844386645206}

    To convert this to a dataframe, I ran the following:

    df = pd.DataFrame.from_dict(get_max_path(2), orient = 'index').reset_index()

    Returns a simple two column dataframe with a separate index:

    index 0 0 2 0.309750 1 3 0.441318

    Just rename the columns using f.rename(columns={'index': 'Column1', 0: 'Column2'}, inplace=True)

提交回复
热议问题