[英]Converting a Dictionary to DataFrame in Python
I have a dictionary of a static structure:我有一个静态结构的字典:
Key: Key: Value`
I will need to record data a few extra keys deep to the same depth , so somewhat uniform.我将需要深记录数据的一些额外的按键相同的深度,所以有些均匀。
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",
}
}
I want a DataFrame of this structure:我想要一个这种结构的 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
Example Desired DataFrame:所需数据帧示例:
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
Where all Child Values are either a list object of strings or a string object.其中所有子值都是字符串列表对象或字符串对象。
From researching I found pandas.DataFrame.from_dict() .通过研究,我发现了pandas.DataFrame.from_dict() 。 However neither orient
values help in my case.然而,在我的情况下, orient
价值观都没有帮助。 As it is intended for flat dictionaries.因为它适用于平面词典。
I genuinely haven't a clue on how to approach this.我真的不知道如何解决这个问题。 What a simple libraries may be needed etc.可能需要什么简单的库等。
Please let me know if there are further details/ nuances I could clarify.如果有我可以澄清的更多细节/细微差别,请告诉我。
Use:用:
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)
Output输出
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
A simple way to do this would just be to "flatten" your dictionary so that you get the "parent, child key, child value" structure you are hoping for and then construct a DataFrame from that.一个简单的方法就是“展平”你的字典,这样你就可以得到你想要的“父、子键、子值”结构,然后从中构造一个 DataFrame。
Example:例子:
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))
The result looks like:结果如下:
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.