I have a json file like this:
{
"A": {
"type1": [
2,
26,
288,
...
],
"type2": [
2,
3,
5,
...
],
"type3": [
23,
26,
288,
...
]
},
"B": {
"type1": [
2,
26,
288,
...
],
"type2": [
2,
3,
5,
...
],
"type3": [
23,
26,
288,
...
]
},
...
"K": {
"type1": [
2,
26,
288,
...
],
"type2": [
2,
3,
5,
...
],
"type3": [
23,
26,
288,
...
]
}
}
Here is a sample dictionary of it for replication (I have about 20 columns from A to J, but only 3 types: type1,type2,type3):
pd.DataFrame({'A': {'type1': ['32',
'21',
'43',
'43',
'43',
'43',
'43',
'43'],
'type2': [
0.133333333333333,
0.36666666666666703,
0.1,
0.30000000000000004,
0.16666666666666702,
0.033333333333333,
0.2,
0.066666666666666],
'type3': [
3,
9,
5,
1,
6,
2]},
'B': {'type1': [
'43',
'43',
'43',
'43',
'43'],
'type2': [
0.23333333333333303,
0.266666666666667,
0.30000000000000004,
0.5666666666666671,
0.16666666666666702,
0.266666666666667],
'type3': [
10,
6,
17,
7,
8,
9,
17,
5,
8]},
'C': {'type1': [
'43',
'43',
'43',
'43',
'43',
'43'],
'type2': [
5.23333333333333,
6.1,
5.4,
3.23333333333333,
17.4,
5.56666666666667,
10.4333333333333,
2.1],
'type3': [
183,
162,
97,
522,
167,
313,
63]},
})
My df should look like this in the end:
type1 type2 type3
A 32 0.13 3
A 21 0.36 9
A
........
B
........
C
........
J
So I transposed it:
But then I'm unsure how to unpack those lists. Every time I try I end up with some loops:
for x in df.index:
for y in df.loc[x]["type1"]:
df.iloc[index] = df.append({"index": x, "type": y}, ignore_index=True)
index += 1
and it is obviously not the way to go.
I was wondering if there is a easier way, maybe as I load the json?
Thanks!