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!
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.
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