For most use cases, you are not recommended to store dictionaries in a DataFrame.
Another value worth mentioning is Panel .
Suppose you have a DataFrame of dictionaries (with fairly consistent keys):
In [11]: df = pd.DataFrame([[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], [{'a': 5, 'b': 6}, {'a': 7, 'b': 8}]], columns=list('AB')) In [12]: df Out[12]: AB 0 {'a': 1, 'b': 2} {'a': 3, 'b': 4} 1 {'a': 5, 'b': 6} {'a': 7, 'b': 8}
You can create a panel (note that there are more direct / preferred ways to create it!):
In [13]: wp = pd.Panel({'A': df['A'].apply(pd.Series), 'B': df['B'].apply(pd.Series)}) In [14]: wp Out[14]: <class 'pandas.core.panel.Panel'> Dimensions: 2 (items) x 2 (major_axis) x 2 (minor_axis) Items axis: A to B Major_axis axis: 0 to 1 Minor_axis axis: a to b
Sections that can be accessed efficiently like DataFrames in various ways, for example:
In [15]: wp.A Out[15]: ab 0 1 2 1 5 6 In [16]: wp.minor_xs('a') Out[16]: AB 0 1 3 1 5 7 In [17]: wp.major_xs(0) Out[17]: AB a 1 3 b 2 4
So you can do all the pandas hedging of a DataFrame:
In [18]: wp.A.plot() # easy! Out[18]: <matplotlib.axes.AxesSubplot at 0x1048342d0>
There are also (“experimental”) panels with a higher size .
source share