I have json data below
[[{"value":"ZZ","formattedValue":"ZZ"},{"value":"In","formattedValue":"In"},{"value":"Amount1","formattedValue":"Amount1"},{"value":"100","formattedValue":"100"}],[{"value":"ZZ","formattedValue":"ZZ"},{"value":"In","formattedValue":"In"},{"value":"Amount2","formattedValue":"Amount2"},{"value":"200","formattedValue":"200"}],[{"value":"ZZ","formattedValue":"ZZ"},{"value":"Out","formattedValue":"Out"},{"value":"Amount1","formattedValue":"Amount1"},{"value":"30","formattedValue":"30"}],[{"value":"ZZ","formattedValue":"ZZ"},{"value":"Out","formattedValue":"Out"},{"value":"Amount2","formattedValue":"Amount2"},{"value":"4","formattedValue":"40"}],[{"value":"CC","formattedValue":"CC"},{"value":"In","formattedValue":"In"},{"value":"Amount1","formattedValue":"Amount1"},{"value":"100","formattedValue":"100"}],[{"value":"CC","formattedValue":"CC"},{"value":"In","formattedValue":"In"},{"value":"Amount2","formattedValue":"Amount2"},{"value":"200","formattedValue":"200"}],[{"value":"CC","formattedValue":"CC"},{"value":"Out","formattedValue":"Out"},{"value":"Amount1","formattedValue":"Amount1"},{"value":"30","formattedValue":"30"}],[{"value":"CC","formattedValue":"CC"},{"value":"Out","formattedValue":"Out"},{"value":"Amount2","formattedValue":"Amount2"},{"value":"4","formattedValue":"40"}]]
In the table format, it should look like below,
| Dimension | Type | Amount1 | Amount2 |
|---|---|---|---|
| ZZ | In | 100 | 200 |
| ZZ | Out | 30 | 40 |
| CC | In | 100 | 200 |
| CC | Out | 30 | 40 |
howerver, if I convert the json using python code below, the order in which the incoming data came is not preserved.
data = 'jsondata'
data = json.loads(data)
df = pd.DataFrame(data).stack().map(lambda x:x.get('formattedValue')).unstack()
df.columns =['Type','InOut','MeasureName','MeasureValue']
df = df.pivot_table(index=['Type','InOut'],columns=['MeasureName'],values="MeasureValue",aggfunc='sum').reset_index()
the output of this is below. If you look at it, the dimension is ordered. I dont want it to happen. I have to keep the order of the data same as it came in. How to achieve this please? Thanks.
| Dimension | Type | Amount1 | Amount2 |
|---|---|---|---|
| CC | In | 100 | 200 |
| CC | Out | 30 | 40 |
| ZZ | In | 100 | 200 |
| ZZ | Out | 30 | 40 |