KeyError使用熊猫读取嵌套的JSON

时间:2018-12-25 21:33:26

标签: python json pandas python-2.7 keyerror

我试图将JSON文件中嵌套属性的值赋给os。该文件是:

{ "_id" : { "$oid" : "5b9058462f38434ab0d85cd3" }, "user_day_code" : "ead1db07fa526e19fe237115d5516fbdc5acb99057b885e8f662a147990b3c4b", "idplug_base" : 5, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.7073786, 40.4237274997222 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE SAN BERNARDO 38,Madrid", "speed" : 1.75, "secondsfromstart" : 205 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.709896, 40.4191897997222 ] }, "type" : "Feature", "properties" : { "var" : "28013,ES,Madrid,Madrid,CUSTA SANTO DOMINGO 6,Madrid", "speed" : 4.63, "secondsfromstart" : 85 } } ] }, "user_type" : 1, "idunplug_base" : 17, "travel_time" : 263, "idunplug_station" : 40, "ageRange" : 0, "idplug_station" : 16, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "" }
{ "_id" : { "$oid" : "5b9058462f38434ab0d85ce9" }, "user_day_code" : "420d9e220bd8816681162e15e9afcb1c69c5a756090728701083c5c0b23502f2", "idplug_base" : 12, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.7022001, 40.4052982997222 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,GTA EMBAJADORES,Madrid", "speed" : 0.33, "secondsfromstart" : 351 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.698618, 40.4061700997222 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,RONDA ATOCHA 30,Madrid", "speed" : 6.36, "secondsfromstart" : 291 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.6949231, 40.4072785997222 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,RONDA ATOCHA,Madrid", "speed" : 4.77, "secondsfromstart" : 231 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.6920543, 40.4081501 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,PLAZA EMPERADOR CARLOS V 1,Madrid", "speed" : 4.38, "secondsfromstart" : 170 } } ] }, "user_type" : 1, "idunplug_base" : 26, "travel_time" : 382, "idunplug_station" : 85, "ageRange" : 2, "idplug_station" : 52, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "28009" }
{ "_id" : { "$oid" : "5b9058462f38434ab0d85ced" }, "user_day_code" : "780f5c8157efe8e6dca44dbd689817d4b126364fca917f0e668bad9e7bf96939", "idplug_base" : 1, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.69610249972222, 40.427829 ] }, "type" : "Feature", "properties" : { "var" : "28004,ES,Madrid,Madrid,PLAZA ALONSO MARTINEZ,Madrid", "speed" : 6.22, "secondsfromstart" : 200 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.69482799972222, 40.4282634997222 ] }, "type" : "Feature", "properties" : { "var" : "28010,ES,Madrid,Madrid,CALLE FERNANDO EL SANTO 4,Madrid", "speed" : 0, "secondsfromstart" : 140 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.69164359972222, 40.4280088 ] }, "type" : "Feature", "properties" : { "var" : "28010,ES,Madrid,Madrid,CALLE FERNANDO EL SANTO 20,Madrid", "speed" : 5.05, "secondsfromstart" : 80 } } ] }, "user_type" : 1, "idunplug_base" : 11, "travel_time" : 305, "idunplug_station" : 109, "ageRange" : 4, "idplug_station" : 58, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "28004" }
{ "_id" : { "$oid" : "5b9058462f38434ab0d85cee" }, "user_day_code" : "a225ab7b4b74954cd9fbe8cc2ec63390cd04e92cdd1a2fe1e58d42faea082b21", "idplug_base" : 1, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.72050759972222, 40.4277548 ] }, "type" : "Feature", "properties" : { "var" : "28008,ES,Madrid,Madrid,PASEO PINTOR ROSALES 49P,Madrid", "speed" : 0.86, "secondsfromstart" : 258 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.717881, 40.4274713 ] }, "type" : "Feature", "properties" : { "var" : "28008,ES,Madrid,Madrid,CALLE QUINTANA 17,Madrid", "speed" : 6.75, "secondsfromstart" : 199 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.7142441, 40.4297779997222 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE SERRANO JOVER 4D,Madrid", "speed" : 7.08, "secondsfromstart" : 139 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.71240559972222, 40.4341422997222 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE FERNANDO EL CATOLICO 47A,Madrid", "speed" : 5.25, "secondsfromstart" : 79 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.7089558, 40.4340593 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE FERNANDO EL CATOLICO 21,Madrid", "speed" : 5.61, "secondsfromstart" : 19 } } ] }, "user_type" : 1, "idunplug_base" : 1, "travel_time" : 262, "idunplug_station" : 168, "ageRange" : 4, "idplug_station" : 120, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "28015" }

使用的代码是:

d100 =  pd.read_json('test 1.json', lines=True)
d100["track"]["features"][0]["geometry"]["coordinates"]

尽管发现Key似乎正确,但仍检索到KeyError

/home/cloudera/anaconda2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in get_value(self, series, key)
2558         try:
2559             return self._engine.get_value(s, k,
-> 2560
tz=getattr(series.dtype, 'tz', None))
2561         except KeyError as e1:
2562             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:


pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

KeyError: 'features'

对此有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

您的#include <include/v8.h> #include <include/libplatform/libplatform.h> using v8::HandleScope; using v8::Isolate; using v8::Local; using v8::Promise; using v8::WasmModuleObjectBuilderStreaming; using v8::WasmCompiledModule; using v8::Context; using v8::Local; using v8::Value; using v8::String; using v8::Object; using v8::Function; using v8::Int32; using args_type = Local<Value>[]; int main(int argc, char* argv[]) { v8::V8::InitializeICUDefaultLocation(argv[0]); v8::V8::InitializeExternalStartupData(argv[0]); std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform(); v8::V8::InitializePlatform(platform.get()); v8::V8::Initialize(); Isolate::CreateParams create_params; create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); Isolate* isolate = Isolate::New(create_params); Isolate::Scope isolate_scope(isolate); HandleScope scope(isolate); Local<Context> context = Context::New(isolate); Context::Scope context_scope(context); WasmModuleObjectBuilderStreaming stream(isolate); // Use the v8 API to generate a WebAssembly module. // // |bytes| contains the binary format for the following module: // // (func (export "add") (param i32 i32) (result i32) // get_local 0 // get_local 1 // i32.add) // // taken from: https://github.com/v8/v8/blob/master/samples/hello-world.cc#L66 std::vector<uint8_t> wasmbin { 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b }; // same as calling: // let module = new WebAssembly.Module(bytes); Local<WasmCompiledModule> module = WasmCompiledModule::DeserializeOrCompile(isolate, WasmCompiledModule::BufferReference(0, 0), WasmCompiledModule::BufferReference(wasmbin.data(), wasmbin.size()) ).ToLocalChecked(); // same as calling: // let module_instance_exports = new WebAssembly.Instance(module).exports; args_type instance_args{module}; Local<Object> module_instance_exports = context->Global() ->Get(context, String::NewFromUtf8(isolate, "WebAssembly")) .ToLocalChecked().As<Object>() ->Get(context, String::NewFromUtf8(isolate, "Instance")) .ToLocalChecked().As<Object>() ->CallAsConstructor(context, 1, instance_args) .ToLocalChecked().As<Object>() ->Get(context, String::NewFromUtf8(isolate, "exports")) .ToLocalChecked().As<Object>() ; // same as calling: // module_instance_exports.add(77, 88) args_type add_args{Int32::New(isolate, 77), Int32::New(isolate, 88)}; Local<Int32> adder_res = module_instance_exports ->Get(context, String::NewFromUtf8(isolate, "add")) .ToLocalChecked().As<Function>() ->Call(context, context->Global(), 2, add_args) .ToLocalChecked().As<Int32>(); printf("77 + 88 = %d\n", adder_res->Value()); return 0; } 是您的d100["track"]完整列。 e。 系列:

d100

它有四个项(行),但是您未指定任何项

所以不是

In[1]: d100["track"]                                                                
Out[1]:                                                                             
0    {'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coord.....
1    {'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coord.....
2    {'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coord.....
3    {'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coord.....
Name: track, dtype: object      

使用(针对d100["track"]["features"][0]["geometry"]["coordinates"] 系列中的单个项目)

d100["track"]

获得

d100["track"][0]["features"][0]["geometry"]["coordinates"] 
d100["track"][1]["features"][0]["geometry"]["coordinates"] 
d100["track"][2]["features"][0]["geometry"]["coordinates"] 
d100["track"][3]["features"][0]["geometry"]["coordinates"]