【问题标题】:How to convert nested dictionary into a 2D table [duplicate]如何将嵌套字典转换为二维表 [重复]
【发布时间】:2015-04-04 19:09:05
【问题描述】:

如何将嵌套字典转换为二维表

data[0]是表格行的集合

data[0][0]是表格行,

key:year是列名,

key:values 是列中的值。

我想将 data[0] 恢复为 Pandas 数据框中的表格形式。

我发现 json_normalize 可能会有所帮助,但不知道该怎么做。

有什么建议吗?

预期格式

字典中的原始数据

(Pdb++) data[0]
    [{u'values': [{u'Actual': u'(0.2)'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2009'}, {u'values': [{u'Actual': u'2.8'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2010'}, {u'values': [{u'Actual': u'2.0'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2011'}, {u'values': [{u'Actual': u'2.0'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2012'}, {u'values': [{u'Actual': u'2.5'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2013'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'3.0'}, {u'Upper End of Central Tendency': u'3.0'}, {u'Lower End of Central Tendency': u'2.8'}, {u'Lower End of Range': u'2.1'}], u'year': u'2014'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'3.5'}, {u'Upper End of Central Tendency': u'3.2'}, {u'Lower End of Central Tendency': u'3.0'}, {u'Lower End of Range': u'2.2'}], u'year': u'2015'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'3.4'}, {u'Upper End of Central Tendency': u'3.0'}, {u'Lower End of Central Tendency': u'2.5'}, {u'Lower End of Range': u'2.2'}], u'year': u'2016'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'2.4'}, {u'Upper End of Central Tendency': u'2.3'}, {u'Lower End of Central Tendency': u'2.2'}, {u'Lower End of Range': u'1.8'}], u'year': u'Longer Run'}]

(Pdb++) data[0][0]
{u'values': [{u'Actual': u'(0.2)'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2009'}

也许改变 JSON 模式会是一个更好的解决方案?

如果是这样,哪种新的 JSON 模式设计更适合这种表数据。谢谢

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    import pandas
    
    # set up data structures
    columns = [
        "year",
        "actual",
        "upper",
        "upper_central",
        "lower_central",
        "lower"
    ]
    value_getter = {
        "year"         : lambda item: item['year'],
        "actual"       : lambda item: item['values'][0]['Actual'],
        "upper"        : lambda item: item['values'][1]['Upper End of Range'],
        "upper_central": lambda item: item['values'][2]['Upper End of Central Tendency'],
        "lower_central": lambda item: item['values'][3]['Lower End of Central Tendency'],
        "lower"        : lambda item: item['values'][4]['Lower End of Range']
    }
    mydata = {
        "year"         : [],
        "actual"       : [],
        "upper"        : [],
        "upper_central": [],
        "lower_central": [],
        "lower"        : []
    }
    
    # repackage data
    for item in data[0]:
        for column in columns:
            mydata[column].append(value_getter[column](item))
    
    # and stuff it into pandas
    df = pandas.DataFrame(mydata, columns=columns)
    

    然后df.T 给出

                       0     1     2     3     4     5     6     7           8
    year            2009  2010  2011  2012  2013  2014  2015  2016  Longer Run
    actual         (0.2)   2.8   2.0   2.0   2.5     -     -     -           -
    upper              -     -     -     -     -   3.0   3.5   3.4         2.4
    upper_central      -     -     -     -     -   3.0   3.2   3.0         2.3
    lower_central      -     -     -     -     -   2.8   3.0   2.5         2.2
    lower              -     -     -     -     -   2.1   2.2   2.2         1.8
    

    【讨论】:

    • 您好,您认为更改 json 架构会是更好的解决方案吗?如果是这样,什么样的json模式设计更适合这种表数据。谢谢~
    • 我试过你的解决方案,它有效~非常感谢。我以这种格式更改 JSON 方案stackoverflow.com/questions/28340557/… 怎么办?谢谢
    【解决方案2】:

    为了提高效率,您应该初始化数据框,但如果您的数据集很小,并且您不知道最里面的字典中出现的所有可能字符串,则无需这样做。

     import pandas as pd
     df=pd.DataFrame
     for dict1 in data[0]:
         for dict2 in dict1['values']:
             for key,val in zip(dict2.keys(),dict2.values()):
                    df.loc[key,dict1['year']]=val
    df
    

    【讨论】:

      猜你喜欢
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2019-09-17
      • 2023-03-11
      • 1970-01-01
      • 2022-12-05
      相关资源
      最近更新 更多