Pandas: read_json Changes Data

Pandas: read_json Changes Data



I have a json file that I'm trying to read into Pandas. The file looks like this:


json


"0": "a": 0, "b": "some_text", "c": "other_text",
"1": "a": 1, "b": "some_text1", "c": "other_text1",
"2": "a": 2, "b": "some_text2", "c": "other_text2"



When I do:


df = pd.read_json("my_file.json")
df = df.transpose()
df.head()



I see:


a b c
0 0 some_text other_text
1 1 some_text1 other_text1
10 10 some_text2 other_text2



So the dataframe's index and column a have somehow gotten mangled in the process. What am I doing incorrectly?


a



Thanks!





By default, it treats the first column as the index. You can do reset_index to reset the index
– mad_
Aug 23 at 17:33





I think the problem was introduced when you saved the DataFrame. You can always do set_index('a') to fix it.
– coldspeed
Aug 23 at 17:38


set_index('a')





Could you provide its version? I'm not able to reproduce this behavior. (pandas=0.23.4, python=3.6.6)
– vhcandido
Aug 23 at 17:41





@mad_ @coldspeed Thanks! However, my json keys and the a column were changed. If you look at the 3rd entry in my json file, the key is "2" and the value associated with "a" is 2 but both get changed to 10...
– bclayman
Aug 23 at 17:42


a


"2"


"a"





@vhcandido Python 2.7.14, pandas 0.19
– bclayman
Aug 23 at 17:42









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)