1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python pandas dataframe 转json_python-将嵌套的json转换为pandas dataframe

python pandas dataframe 转json_python-将嵌套的json转换为pandas dataframe

时间:2020-12-14 12:06:37

相关推荐

python pandas dataframe 转json_python-将嵌套的json转换为pandas dataframe

时间: -10-27 07:33:05

标签:

pandas

python

我正在尝试将嵌套的json数组转换为 pandas dataframe .

列表格式的数据如下所示:

[{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'}}],

有没有建议我如何将其转换为 dataframe 并处理空值?我尝试使用try /除了处理丢失的值,但是我的 dataframe 然后由元组组成.

谢谢

最佳答案

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]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。