I have a dictionary of a static structure:
JavaScript
x
2
1
Key: Key: Value`
2
I will need to record data a few extra keys deep to the same depth, so somewhat uniform.
Example Dictionary:
JavaScript
1
26
26
1
{
2
"Emissions": {
3
"305-1": [
4
"2014_249989",
5
"2015_339998",
6
"2016_617957",
7
"2017_827230"
8
],
9
"305-2": [
10
"2014_33163",
11
"2015_64280",
12
"2016_502748",
13
"2017_675091"
14
],
15
},
16
"Effluents and Waste": {
17
"306-1": [
18
"2014_143.29",
19
"2015_277.86",
20
"2016_385.67",
21
"2017_460.6"
22
],
23
"306-2": "blah blah blah",
24
}
25
}
26
I want a DataFrame of this structure:
JavaScript
1
5
1
Parent Key | Child Key | Child Value
2
Parent Key | Child Key | Child Value
3
Parent Key | Child Key | Child Value
4
Parent Key | Child Key | Child Value
5
Example Desired DataFrame:
JavaScript
1
5
1
Emissions | 305-1 | ["2014_249989", "2015_339998", "2016_617957", "2017_827230"]
2
Emissions | 305-2 | ["2014_33163", "2015_64280", "2016_502748", "2017_675091"]
3
Effluents and Waste| 306-1 | ["2014_249989", "2015_339998", "2016_617957", "2017_827230"]
4
Effluents and Waste | 306-2 | blah blah blah
5
Where all Child Values are either a list object of strings or a string object.
From researching I found pandas.DataFrame.from_dict(). However neither orient
values help in my case. 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.
Advertisement
Answer
Use:
JavaScript
1
17
17
1
import pandas as pd
2
3
data = {
4
"Emissions": {
5
"305-1": ["2014_249989", "2015_339998", "2016_617957", "2017_827230"],
6
"305-2": ["2014_33163", "2015_64280", "2016_502748", "2017_675091"],
7
},
8
"Effluents and Waste": {
9
"306-1": ["2014_143.29", "2015_277.86", "2016_385.67", "2017_460.6"],
10
"306-2": "blah blah blah",
11
}
12
}
13
14
data = [[key, ikey, value] for key, values in data.items() for ikey, value in values.items()]
15
res = pd.DataFrame(data)
16
print(res)
17
Output
JavaScript
1
6
1
0 2
2
0 Emissions [2014_249989, 2015_339998, 2016_617957, 2017_8
3
1 Emissions [2014_33163, 2015_64280, 2016_502748, 2017_675
4
2 Effluents and Waste [2014_143.29, 2015_277.86, 2016_385.67, 2017_4
5
3 Effluents and Waste blah blah blah
6