字典的字典对大 pandas 数据框的字典-将多索引行更改为列 [英] Dict of dict of dicts to pandas dataframe - changing multiindex rows to be columns

查看:97
本文介绍了字典的字典对大 pandas 数据框的字典-将多索引行更改为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本这样的字典:

my_dict = {'Key': {'Service': {'Number': 61, 'Percent': 2.54 }, 'Service2': {'Number': 42, 'Percent': 2.2 } }, 'Key2': {'Service3': {'Number': 8, 'Percent': 2.74}, 'Service2': {'Number': 52, 'Percent': 2.5 } }}

我正在尝试将其转换为熊猫数据框.我有这个解决方案可以工作

I'm trying to convert this to a pandas dataframe. I got this solution to work

pandas.concat(map(pandas.DataFrame, my_dict.itervalues()), keys=my_dict.keys()).stack().unstack(0)

但是,我的问题是我得到一个表,其中行索引是Service& Service的多指标.数字/百分比.相反,我希望索引仅是出现的不同服务(而不是多索引),并且希望这些列像现在一样是键,但其中第1列部分是Number,第2列部分是所有Keys百分比,如果有道理的话.转置不是我想要的,因为我不希望整个索引更改,而只是更改Number/Percent部分.从上面我写的字典将其转换为数据框后,我希望它看起来像这样:

However, my problem is that that I get a table where the row index is a multindex of Service & Number/Percent. Instead, I want the index to be only the different Services that come up (not a multiindex), and want the columns to be the Keys like they are now, but with 1 column section being Number and the 2nd column section being all the Keys with percent, if that makes sense. Transposing is not what I want, because I don't want the entire index to change, just the Number/Percent part. I want it to look like this, after converting it to a dataframe from the dictionary I wrote above:

          Number         Percent
          Key    Key2    Key     Key2
Service   61     NaN     2.54    NaN
Service2  42     52      2.2     2.5
Service3  NaN    8       NaN     2.74

对此有何建议?

推荐答案

pd.concat({k: pd.DataFrame(v) for k, v in my_dict.items()})

              Service  Service2  Service3
Key  Number     61.00      42.0       NaN
     Percent     2.54       2.2       NaN
Key2 Number       NaN      52.0      8.00
     Percent      NaN       2.5      2.74


pd.concat({k: pd.DataFrame(v) for k, v in my_dict.items()}, axis=1).stack(0).T

         Number       Percent      
            Key  Key2     Key  Key2
Service    61.0   NaN    2.54   NaN
Service2   42.0  52.0    2.20  2.50
Service3    NaN   8.0     NaN  2.74


这不依赖于理解


This doesn't rely on comprehensions

pd.DataFrame(my_dict).stack().apply(pd.Series).unstack()
# pandas.DataFrame(i).stack().apply(pandas.Series).unstack()

         Number       Percent      
            Key  Key2     Key  Key2
Service    61.0   NaN    2.54   NaN
Service2   42.0  52.0    2.20  2.50
Service3    NaN   8.0     NaN  2.74

这篇关于字典的字典对大 pandas 数据框的字典-将多索引行更改为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆