Python: Transform weird dictionary into pandas - dataframe

I have a dictionary that looks as this:

result = 
{'Var1': [2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,0.0,0.0,0.0,0.0],
 'Var2': [2.0,2.0,2.0,0.0,2.0,2.0,2.0,0.0,2.0,2.0,2.0,0.0,2.0,2.0,2.0,0.0]}

and I need to get a dataframe that looks as follows:

   Var1   Var2
1   2.0    2.0
2   2.0    2.0
3   2.0    2.0
4   2.0    0.0
..   ..     ..

I tried to do this:

pd.DataFrame(result.items(), columns=['A', 'B']).T

and I'm getting something similar to this:

                           0                             1
A                        Var1                         Var2
B   [2.0, 2.0, 2.0, 2.0, ...]    [2.0, 2.0, 2.0, 0.0, ...]

Can somebody guide me how to solve this issue? Thanks!

4 answers

  • answered 2018-11-08 05:45 Naga Kiran

    You can use Pandas Dataframe from_dict to read the dictionary

    pd.DataFrame.from_dict(result)
    

    edit

    orient : {‘columns’, ‘index’}

    The “orientation” of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’ (default). Otherwise if the keys should be rows, pass ‘index’.

    pd.DataFrame.from_dict(result,orient='columns')
    

    Out:

        Var1    Var2
    0   2.0 2.0
    1   2.0 2.0
    2   2.0 2.0
    3   2.0 0.0
    4   2.0 2.0
    5   2.0 2.0
    6   2.0 2.0
    7   2.0 0.0
    8   2.0 2.0
    9   2.0 2.0
    10  2.0 2.0
    11  2.0 0.0
    12  0.0 2.0
    13  0.0 2.0
    14  0.0 2.0
    15  0.0 0.0
    

    If you want to use keys information of dictionary as index , you can use orientation of index

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

    Out:

             0  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
    Var1    2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 0.0 0.0 0.0 0.0
    Var2    2.0 2.0 2.0 0.0 2.0 2.0 2.0 0.0 2.0 2.0 2.0 0.0 2.0 2.0 2.0 0.0
    

  • answered 2018-11-08 05:46 DYZ

    All you need is the default constructor:

    pd.DataFrame(result)
    #    Var1  Var2
    #0    2.0   2.0
    #1    2.0   2.0
    #....
    

  • answered 2018-11-08 05:47 Mayank Porwal

    In [1313]: df = pd.DataFrame(result)
    
    In [1314]: df
    Out[1314]: 
        Var1  Var2
    0    2.0   2.0
    1    2.0   2.0
    2    2.0   2.0
    3    2.0   0.0
    4    2.0   2.0
    5    2.0   2.0
    6    2.0   2.0
    7    2.0   0.0
    8    2.0   2.0
    9    2.0   2.0
    10   2.0   2.0
    11   2.0   0.0
    12   0.0   2.0
    13   0.0   2.0
    14   0.0   2.0
    15   0.0   0.0
    

  • answered 2018-11-08 06:40 pygo

    Answered are already rendered, But Just would like to make it to understand How it works.

    While Constructing DataFrame

    • Pandas DataFrame may contains the different type of data like Two-dimensional tabular data structure with labeled axes (rows and columns) or as a dict-like container for Series objects.

    Example attributes(Parameters) it has..

     DataFrame(data=None, index=None, columns=None, dtype=None,copy=False)
    
    • look at the Parameters section of the doc, So, while Constructing DataFrame from a dictionary you can use Parameter data

    So, in your case it will be suitable to use parameter data while building a dataframe from dict object ..

    >>> pd.DataFrame(data=result)
        Var1  Var2
    0    2.0   2.0
    1    2.0   2.0
    2    2.0   2.0
    3    2.0   0.0
    4    2.0   2.0
    5    2.0   2.0
    6    2.0   2.0
    7    2.0   0.0
    8    2.0   2.0
    9    2.0   2.0
    10   2.0   2.0
    11   2.0   0.0
    12   0.0   2.0
    13   0.0   2.0
    14   0.0   2.0
    15   0.0   0.0
    

    OR

    >>> pd.DataFrame.from_dict(result)
    >>> pd.DataFrame.from_dict(result, orient='index') # Otherwise if the keys should be rows, pass 'index'
    
    • Above should be the columns of the resulting DataFrame, pass 'columns' (default). Otherwise if the keys should be rows, pass 'index'.