0

Is it possible to keep the 'column' order in a Pandas DataFrame after parsing JSON data using json_normalize.

Some of my code:

for row in json_data["results"]["companies"]:
    if df is None:
        df = json_normalize(row["companie"])
    else:
        df = pd.concat([df, json_normalize(row["companie"])])

Now the columns are sorted alphabetically but I want to keep them in the same order as in the JSON data.

John Doe
  • 9,843
  • 13
  • 42
  • 73
  • 2
    You lost the order when you parsed the JSON. Load the JSON data into an `OrderedDict`, see the duplicate. – Martijn Pieters Jan 09 '19 at 12:50
  • Many thanks for pointing me in the right direction. For completeness and future use : `json_data = requests.get(url).json(object_pairs_hook=OrderedDict)` – John Doe Jan 10 '19 at 06:07

0 Answers0