簡體   English   中英

在 Python 中將字典轉換為 DataFrame

[英]Converting a Dictionary to DataFrame in Python

我有一個靜態結構的字典:

Key: Key: Value`

我將需要記錄數據的一些額外的按鍵相同的深度,所以有些均勻。

示例詞典:

{
    "Emissions": {
        "305-1": [
            "2014_249989",
            "2015_339998",
            "2016_617957",
            "2017_827230"
        ],
        "305-2": [
            "2014_33163",
            "2015_64280",
            "2016_502748",
            "2017_675091"
        ],
    },
    "Effluents and Waste": {
        "306-1": [
            "2014_143.29",
            "2015_277.86",
            "2016_385.67",
            "2017_460.6"
        ],
        "306-2": "blah blah blah",
    }
}

我想要一個這種結構的 DataFrame:

Parent Key | Child Key | Child Value
Parent Key | Child Key | Child Value
Parent Key | Child Key | Child Value
Parent Key | Child Key | Child Value

所需數據幀示例:

Emissions | 305-1 | ["2014_249989", "2015_339998", "2016_617957", "2017_827230"]
Emissions | 305-2 | ["2014_33163", "2015_64280", "2016_502748", "2017_675091"]
Effluents and Waste| 306-1 | ["2014_249989", "2015_339998", "2016_617957", "2017_827230"]
Effluents and Waste | 306-2 | blah blah blah

其中所有子值都是字符串列表對象或字符串對象。


通過研究,我發現了pandas.DataFrame.from_dict() 然而,在我的情況下, orient價值觀都沒有幫助。 因為它適用於平面詞典。

我真的不知道如何解決這個問題。 可能需要什么簡單的庫等。

如果有我可以澄清的更多細節/細微差別,請告訴我。

用:

import pandas as pd

data = {
    "Emissions": {
        "305-1": ["2014_249989", "2015_339998", "2016_617957", "2017_827230"],
        "305-2": ["2014_33163", "2015_64280", "2016_502748", "2017_675091"],
    },
    "Effluents and Waste": {
        "306-1": ["2014_143.29", "2015_277.86", "2016_385.67", "2017_460.6"],
        "306-2": "blah blah blah",
    }
}

data = [[key, ikey, value] for key, values in data.items() for ikey, value in values.items()]
res = pd.DataFrame(data)
print(res)

輸出

                     0  ...                                                  2
0            Emissions  ...  [2014_249989, 2015_339998, 2016_617957, 2017_8...
1            Emissions  ...  [2014_33163, 2015_64280, 2016_502748, 2017_675...
2  Effluents and Waste  ...  [2014_143.29, 2015_277.86, 2016_385.67, 2017_4...
3  Effluents and Waste  ...                                     blah blah blah

一個簡單的方法就是“展平”你的字典,這樣你就可以得到你想要的“父、子鍵、子值”結構,然后從中構造一個 DataFrame。

例子:

example_dictionary = {
    "Emissions": {
        "305-1": [
            "2014_249989",
            "2015_339998",
            "2016_617957",
            "2017_827230"
        ],
        "305-2": [
            "2014_33163",
            "2015_64280",
            "2016_502748",
            "2017_675091"
        ],
    },
    "Effluents and Waste": {
        "306-1": [
            "2014_143.29",
            "2015_277.86",
            "2016_385.67",
            "2017_460.6"
        ],
        "306-2": "blah blah blah",
    }
}

def flatten(d):
    return [[key, subkey, d[key][subkey]] for key in d for subkey in d[key]]

pd.DataFrame(flatten(example_dictionary))

結果如下:

0   1   2
0   Emissions   305-1   [2014_249989, 2015_339998, 2016_617957, 2017_8...
1   Emissions   305-2   [2014_33163, 2015_64280, 2016_502748, 2017_675...
2   Effluents and Waste     306-1   [2014_143.29, 2015_277.86, 2016_385.67, 2017_4...
3   Effluents and Waste     306-2   blah blah blah
            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM