I'm trying to make sense of this.
I have this JSON file:
[
{
"id": 1,
"Z": "a",
"C": "z",
"D": "text"
},
{
"id": 2,
"Z": "b",
"C": "y",
"D": "text"
},
{
"id": 3,
"Z": "c",
"C": "x",
"D": "text"
}
]
The order of the data is meaningful and needs to be retained, both for rows and columns. I need to load it to a dataframe. So, here's what happens:
In [1]: import json
In [2]: from pandas.io.json import json_normalize
In [3]: with open('test.json') as f:
...: json_data = json.load(f)
...:
In [4]: json_data
Out[4]:
[{'id': 1, 'Z': 'a', 'C': 'z', 'D': 'text'},
{'id': 2, 'Z': 'b', 'C': 'y', 'D': 'text'},
{'id': 3, 'Z': 'c', 'C': 'x', 'D': 'text'}]
In [5]: df = json_normalize(json_data)
In [6]: df
Out[6]:
C D Z id
0 z text a 1
1 y text b 2
2 x text c 3
As you can see, while rows are still in the right order, columns where sorted alphabetically. How can I keep my column order (or restore it)?
Thanks!