I am familiar with python but new to panda DataFrames. I have a dictionary like this:
JavaScript
2
1
a={'b':100,'c':300}
2
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:
JavaScript
6
1
a={'b':[100,],'c':[300,]}
2
pd.DataFrame(a)
3
4
b c
5
0 100 300
6
Or if for some reason you don’t want to use a list, include an index:
JavaScript
6
1
a={'b':100,'c':300}
2
pd.DataFrame(a, index=['i',])
3
4
b c
5
i 100 300
6