'Create Python DataFrame from dictionary where keys are the column names and values form the row

I am familiar with python but new to panda DataFrames. I have a dictionary like this:

a={'b':100,'c':300}

And I would like to convert it to a DataFrame, where b and c are the column names, and the first row is 100,300 (100 is underneath b and 300 is underneath c). I would like a solution that can be generalized to a much longer dictionary, with many more items. Thank you!



Solution 1:[1]

Pass the values as a list:

a={'b':[100,],'c':[300,]}
pd.DataFrame(a)

     b    c
0  100  300

Or if for some reason you don't want to use a list, include an index:

a={'b':100,'c':300}
pd.DataFrame(a, index=['i',])

     b    c
i  100  300

Solution 2:[2]

Use lists as the values in the dictionary.

import pandas as pd
a = {'b':[100,200],'c':[300,400]}
b = pd.DataFrame(a)

In [4]: b
Out[4]: 
     b    c
0  100  300
1  200  400

Solution 3:[3]

loan_sanction_details = pd.DataFrame(event,index=['i',])

where loan_sanction_details is dataframe and event is dictionary.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iayork
Solution 2 Liam Foley
Solution 3 S.B