Python storing dictionaries inside a data frame

I built a DataFrame pandas that stores a simple dictionary in each cell. For instance:

{'Sales':0,'Revenue':0} 

I can get a specific value from a data frame via:

 df[columnA][index100]['Revenue'] 

But now I would like to plot all Revenue values ​​from dictionaries in columnA - what is the best way to achieve this?

Will life be easier in the long run if I learn dictionaries and use two identical data sizes instead? (I am very new to pandas, so not sure about the best practice).

+6
source share
2 answers

An easy way to get all income values ​​from column A is df[columnA].map(lambda v: v['Revenue']) .

Depending on what you do, life may be easier if you tweak your structure a bit. For example, you can use a hierarchical index with Sales and Revenue as keys at the same level.

+7
source

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 .

+3
source

Source: https://habr.com/ru/post/944823/


All Articles