Why isn't dictionary key order preserved in DataFrame column order in Python 3.7?

Screen Link: https://app.dataquest.io/m/355/list-comprehensions-and-lambda-functions/10/reading-json-files-into-pandas

My Code 1:

json_obj

Output 1 (I understand):

[{'name': 'Sabine', 'age': 36, 'favorite_foods': ['Pumpkin', 'Oatmeal']},
 {'name': 'Zoe',
  'age': 40,
  'favorite_foods': ['Chicken', 'Pizza', 'Chocolate']},
 {'name': 'Heidi', 'age': 40, 'favorite_foods': ['Caesar Salad']}]

My Code 2:

json_df = pd.DataFrame(json_obj)
json_df

Output 2 (I have a question about):

   age               favorite_foods    name
0   36           [Pumpkin, Oatmeal]  Sabine
1   40  [Chicken, Pizza, Chocolate]     Zoe
2   40               [Caesar Salad]   Heidi

My Question:
I’m using Python 3.7. Why isn’t dictionary key order preserved in DataFrame column order in Python 3.7? I thought dictionaries preserved their key order after something like Python 3.6.

Hey.

Are you sure you’re on Python 3.7? I can’t reproduce this on my machine. What is the output of print(__import__("sys").version)?

Hi Bruno,

Screen Shot 2020-03-07 at 9.23.07 PM

Insertion order in dictionaries was only implemented in build 3.7.4.:

image

2 Likes