Pandas read_json() doesn't cast column dtypes

Viewed 38

I'm reading a json file with Pandas read_json(). If I just read it:

input:

df_grades = pd.read_json("students_grades.json")
df_grades.dtypes

output:

names                          object
gender                         object
race/ethnicity                 object
parental level of education    object
lunch                          object
test preparation course        object
math score                     object
reading score                  object
writing score                  object
dtype: object

It just reads every column of the json file as an object. Specifically, columns "math score", "reading score" and "writing score" need to be ints for my purpose. For some reason, using the "dtype" attribute of "read_json" doesn't work.

input:

df_grades = pd.read_json("students_grades.json", dtype={'math score': 'int64',
                                                        'reading score': 'int64',
                                                        'writng score': 'int64'})
df_grades.dtypes

output:

names                          object
gender                         object
race/ethnicity                 object
parental level of education    object
lunch                          object
test preparation course        object
math score                     object
reading score                  object
writing score                  object
dtype: object

Dtypes stay the same for some reason. What can be the reason?

Thanks in advance!

1 Answers
Related