Skip to content
Advertisement

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!

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement