2

I have list of objects as JSON. Each object has two properties: id(string) and arg(number).

When I use pandas.read_json(...), the resulting DataFrame has the id interpreted as number as well, which causes problems, since information is lost.

import pandas as pd

json = '[{ "id" : "1", "arg": 1 },{ "id" : "1_1", "arg": 2}, { "id" : "11", "arg": 2}]'

df = pd.read_json(json)

I'd expect to have a DataFrame like this:

     id     arg  
0   "1"      1
1   "1_1"    2
2   "11"     2

I get

     id     arg  
0     1      1
1    11      2
2    11      2

and suddenly, the once unique id is not so unique anymore.

How can I tell pandas to stop doing that?

My search so far only yielded results, where people where trying to achive the opposite - having columns of string beeing interpreted as numbers. I totally don't want to achive that in this case!

2 Answers 2

4

If you set the dtype parameter to False, read_json will not infer the types automatically:

df = pd.read_json(json, dtype=False)
Sign up to request clarification or add additional context in comments.

Comments

1

Use dtype parameter for preventing cast id to numbers:

df = pd.read_json(json, dtype={'id':str})

print (df)
    id  arg
0    1    1
1  1_1    2
2   11    2


print (df.dtypes)
id     object
arg     int64
dtype: object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.