[英]How will i get the co-ordinate value from the below JSON using python json into a pandas dataframe?
{ "Type": "type1" "Results": [ { "Type": "type1" "Results": [ {"Type":"type1","Coordinate":-0.3,"Value":123123}, {"Type":"type1","Coordinate":-0.2,"Value":123123}, {"Type":"type1","Coordinate":-0.1,"Value":123123}, {"Type":"type1","Coordinate":0,"Value":123123}, {"Type":"type1","Coordinate":0.1,"Value":123123}, {"Type":"type1","Coordinate":0.2,"Value":123123}, {"Type":"type1","Coordinate":0.3,"Value":123123} ] } ] } {“类型”:“type1”“结果”:[{“类型”:“type1”“结果”:[{“类型”:“type1”,“坐标”:-0.3,“值”:123123},{ "类型":"type1","坐标":-0.2,"值":123123}, {"类型":"type1","坐标":-0.1,"值":123123}, {"类型": "type1","Coordinate":0,"Value":123123}, {"Type":"type1","Coordinate":0.1,"Value":123123}, {"Type":"type1","Coordinate ":0.2,"Value":123123}, {"Type":"type1","Coordinate":0.3,"Value":123123} ] } ] }
>>> d = json.load(open('results.json'))
>>> d['Results'][0]['Results'][0]['Coordinate']
-0.3
>>> d['Results'][0]['Results'][1]['Coordinate']
-0.2
You can iterate through those, or simply pass pandas that final Results
:您可以遍历这些,或者简单地将 pandas 传递给最终
Results
:
>>> df = pd.DataFrame(d['Results'][0]['Results'])
>>> df.dtypes
Type object
Coordinate float64
Value int64
You apparently intended to use the following JSON input, which is a little different from what you posted.您显然打算使用以下 JSON 输入,这与您发布的内容略有不同。
{"Results": [{"Results": [{"Coordinate": -0.3, "Type": "type1", "Value": 123123},
{"Coordinate": -0.2, "Type": "type1", "Value": 123123},
{"Coordinate": -0.1, "Type": "type1", "Value": 123123},
{"Coordinate": 0, "Type": "type1", "Value": 123123},
{"Coordinate": 0.1, "Type": "type1", "Value": 123123},
{"Coordinate": 0.2, "Type": "type1", "Value": 123123},
{"Coordinate": 0.3, "Type": "type1", "Value": 123123}],
"Type": "type1"}],
"Type": "type1"}
First, the JSON
you have posted is not valid JSON
.首先,您发布的
JSON
无效JSON
。 The correct version is正确的版本是
{
"Type" : "type1",
"Results" :
[
{ "Type" : "type1", "Results" :
[
{"Type":"type1","Coordinate":-0.3,"Value":123123},
{"Type":"type1","Coordinate":-0.2,"Value":123123},
{"Type":"type1","Coordinate":-0.1,"Value":123123},
{"Type":"type1","Coordinate":0,"Value":123123},
{"Type":"type1","Coordinate":0.1,"Value":123123},
{"Type":"type1","Coordinate":0.2,"Value":123123},
{"Type":"type1","Coordinate":0.3,"Value":123123}
]
}
]
}
Please note the missing , that are missing in you post.请注意您的帖子中缺少的, 。 I don't know exactly what you want, but please have a look at the following.
我不知道你到底想要什么,但请看看下面的内容。
import pandas as pd
json = {
"Type" : "type1",
"Results" :
[
{ "Type" : "type1", "Results" :
[
{"Type":"type1","Coordinate":-0.3,"Value":123123},
{"Type":"type1","Coordinate":-0.2,"Value":123123},
{"Type":"type1","Coordinate":-0.1,"Value":123123},
{"Type":"type1","Coordinate":0,"Value":123123},
{"Type":"type1","Coordinate":0.1,"Value":123123},
{"Type":"type1","Coordinate":0.2,"Value":123123},
{"Type":"type1","Coordinate":0.3,"Value":123123}
]
}
]
}
df = pd.DataFrame(json["Results"][0]["Results"])
which gives you这给了你
Type Coordinate Value
0 type1 -0.3 123123
1 type1 -0.2 123123
2 type1 -0.1 123123
3 type1 0.0 123123
4 type1 0.1 123123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.