python,将字典存储在数据框内 [英] python, storing dictionaries inside a dataframe
问题描述
我建立了一个熊猫数据框,在每个单元格中存储一个简单的字典。例如:
I've built a pandas dataframe which is storing a simple dictionary in each cell. For example:
{'Sales':0,'Revenue':0}
我可以通过以下方式从数据框中检索特定值:
I can retrieve a specific value from the dataframe via:
df[columnA][index100]['Revenue']
但现在我想想要在 columnA
中绘制字典中所有收入值的图表-实现此目标的最佳方法是什么?
But now I'd like to plot a graph of all the Revenue values from the dictionaries in columnA
- what is the best way of achieving this?
如果我放弃词典而使用两个相同大小的数据帧,从长远来看,生活会更轻松吗? (对于熊猫来说是很新的人,所以不确定最佳实践。)
Would life be easier in the long run if I dropped the dictionaries and instead used two identically sized dataframes? (Am very new to pandas so not sure of best practice).
推荐答案
一种简单的方法来获取熊猫的所有收益值列A是 df [columnA] .map(lambda v:v ['Revenue'])
。
A simple way to get all the Revenue values from a column A is df[columnA].map(lambda v: v['Revenue'])
.
在您所做的事情上,如果稍微调整一下结构,生活可能确实会更轻松。例如,您可以使用带有销售和收入作为层次结构中的键的层次结构索引。
Depending on what you're doing, life may indeed be easier if you tweak your structure a bit. For instance, you could use a hierarchical index with "Sales" and "Revenue" as the keys in one level.
这篇关于python,将字典存储在数据框内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!