Convert Python dict into a dataframe

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

    Pandas have built-in function for conversion of dict to data frame.

    pd.DataFrame.from_dict(dictionaryObject,orient='index')

    For your data you can convert it like below:

    import pandas as pd
    your_dict={u'2012-06-08': 388,
     u'2012-06-09': 388,
     u'2012-06-10': 388,
     u'2012-06-11': 389,
     u'2012-06-12': 389,
     u'2012-06-13': 389,
     u'2012-06-14': 389,
     u'2012-06-15': 389,
     u'2012-06-16': 389,
     u'2012-06-17': 389,
     u'2012-06-18': 390,
     u'2012-06-19': 390,
     u'2012-06-20': 390,
     u'2012-06-21': 390,
     u'2012-06-22': 390,
     u'2012-06-23': 390,
     u'2012-06-24': 390,
     u'2012-06-25': 391,
     u'2012-06-26': 391,
     u'2012-06-27': 391,
     u'2012-06-28': 391,
     u'2012-06-29': 391,
     u'2012-06-30': 391,
     u'2012-07-01': 391,
     u'2012-07-02': 392,
     u'2012-07-03': 392,
     u'2012-07-04': 392,
     u'2012-07-05': 392,
     u'2012-07-06': 392}
    
    your_df_from_dict=pd.DataFrame.from_dict(your_dict,orient='index')
    print(your_df_from_dict)
    
    0 讨论(0)
  • 2020-11-22 03:52

    I think that you can make some changes in your data format when you create dictionary, then you can easily convert it to DataFrame:

    input:

    a={'Dates':['2012-06-08','2012-06-10'],'Date_value':[388,389]}
    

    output:

    {'Date_value': [388, 389], 'Dates': ['2012-06-08', '2012-06-10']}
    

    input:

    aframe=DataFrame(a)
    

    output: will be your DataFrame

    You just need to use some text editing in somewhere like Sublime or maybe Excel.

    0 讨论(0)
  • 2020-11-22 03:56

    The above answer seems the simplest and works. Copied below.

        dict = {'key 1': 'value 1', 'key 2': 'value 2', 'key 3': 'value 3'}
        df=pd.DataFrame([dict])
        print(df.head())
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 03:57
    pd.DataFrame({'date' : dict_dates.keys() , 'date_value' : dict_dates.values() })
    
    0 讨论(0)
  • 2020-11-22 03:58

    Accepts a dict as argument and returns a dataframe with the keys of the dict as index and values as a column.

    def dict_to_df(d):
        df=pd.DataFrame(d.items())
        df.set_index(0, inplace=True)
        return df
    
    0 讨论(0)
提交回复
热议问题