Using pandas.io.json.json_normalize() with empty list attributes

老子叫甜甜 提交于 2021-02-10 18:05:06

问题


I'm using pandas.io.json.json_normalize() to convert some json into a dataframe, which is then pushed to an SQLite database via df.to_sql().

However, I'm getting sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type. when progressing with this, I think due to one of my json fields being an empty array.

I understand I can pass additional path arguments to json_normalize to have it pull out array values and augment the rows with the parent data:

json_normalize(json_data, 'field_name_of_empty_array_value')

However I can't use this for this instance, as the array is empty and so it returns no values.

Is there a way to set the empty list value to None, or to serialize the empty list via the json_serialize function? Or do I need to do this on the returned dataFrame?

The reason I want to understand whether this is possible is to limit the complexity when dealing with more difficult json which may have empty array values nested deep within its structure.

I've search for this specific question but can't seem to find an example dealing with this specific case, and not sure the docs covers it.

Thanks


回答1:


I believe this isn't possible. So instead, I'm using json_normalize on the full json object, then splitting as required using DataFrame operations, before converting back to json via df.to_json, and using json_normalize with different arguments - specifically, the record_path argument to create records based on the array entries as required.



来源:https://stackoverflow.com/questions/50023807/using-pandas-io-json-json-normalize-with-empty-list-attributes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!