Convert Python dict into a dataframe

前端 未结 16 2572
暗喜
暗喜 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

    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'])
    

提交回复
热议问题