python-将嵌套的json转换为pandas数据框
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-将嵌套的json转换为pandas数据框,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2893字,纯文字阅读大概需要5分钟。
内容图文
![python-将嵌套的json转换为pandas数据框](/upload/InfoBanner/zyjiaocheng/683/c421b3203c35409398c3c0beee65bec5.jpg)
我正在尝试将嵌套的json数组转换为熊猫数据框.
列表格式的数据如下所示:
[{u'analysis': {u'active': u'Y',
u'dpv_cmra': u'N',
u'dpv_footnotes': u'AAN1',
u'dpv_match_code': u'D',
u'dpv_vacant': u'N',
u'footnotes': u'H#'},
u'candidate_index': 0,
u'components':
{u'city_name': u'City',
u'delivery_point': u'Variable',
u'delivery_point_check_digit': u'8',
u'plus4_code': u'Variable',
u'primary_number': u'Variable',
u'state_abbreviation': u'Variable',
u'street_name': u'Variable',
u'street_predirection': u'Variable',
u'street_suffix': u'Variable',
u'zipcode': u'Variable'},
u'delivery_line_1': u'Variable',
u'delivery_point_barcode': u'Variable',
u'input_id': u'Variable',
u'input_index': Variable,
u'last_line': u'Variable',
u'metadata':
{u'building_default_indicator': u'Variable',
u'carrier_route': u'Variable',
u'congressional_district': u'Variable',
u'county_fips': u'Variable',
u'county_name': u'Variable',
u'dst': True,
u'zip_type': u'Variable'}}],
有没有建议我如何将其转换为数据框并处理空值?我尝试使用try /除了处理丢失的值,但是我的数据框然后由元组组成.
谢谢
解决方法:
pd.io.json内部有一个json_normalize函数.
d = {u'analysis': {u'active': u'Y', u'dpv_cmra': u'N', u'dpv_footnotes': u'AAN1', u'dpv_match_code': u'D', u'dpv_vacant': u'N', u'footnotes': u'H#'}, u'candidate_index': 0, u'components': {u'city_name': u'City', u'delivery_point': u'Variable', u'delivery_point_check_digit': u'8', u'plus4_code': u'Variable', u'primary_number': u'Variable', u'state_abbreviation': u'Variable', u'street_name': u'Variable', u'street_predirection': u'Variable', u'street_suffix': u'Variable', u'zipcode': u'Variable'}, u'delivery_line_1': u'Variable', u'delivery_point_barcode': u'Variable', u'input_id': u'Variable', u'input_index': u'Variable', u'last_line': u'Variable', u'metadata': {u'building_default_indicator': u'Variable', u'carrier_route': u'Variable', u'congressional_district': u'Variable', u'county_fips': u'Variable', u'county_name': u'Variable', u'dst': True, u'zip_type': u'Variable'}}
>>> pd.io.json.json_normalize(d)
analysis.active analysis.dpv_cmra analysis.dpv_footnotes analysis.dpv_match_code analysis.dpv_vacant analysis.footnotes candidate_index components.city_name components.delivery_point components.delivery_point_check_digit ... 0 Y N AAN1 D N H# 0 City Variable 8 ...
input_id input_index last_line metadata.building_default_indicator metadata.carrier_route metadata.congressional_district metadata.county_fips metadata.county_name metadata.dst metadata.zip_type
0 Variable Variable Variable Variable Variable Variable Variable Variable True Variable
[1 rows x 29 columns]
内容总结
以上是互联网集市为您收集整理的python-将嵌套的json转换为pandas数据框全部内容,希望文章能够帮你解决python-将嵌套的json转换为pandas数据框所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。