【发布时间】:2021-05-19 14:48:50
【问题描述】:
我一直在尝试规范化这个嵌套的 json,现在已经使用了两天并且没有产生任何输出,我尝试加倍努力,阅读文档但没有显示。我会欣赏额外的手和专业知识。 谢谢。
这些是我试图退出的字段列表;
- 集群时间
- 文档密钥
- fulldocument.buzName
- fulldocument.dealType'
- fulldocument.Companies'
- fulldocument.Sub_companies'
- fulldocument.Directories
- 术语
- '操作类型'
{'_id': {'_data': '826019AB3C000000012B0'},
'clusterTime': Timestamp(1612294972, 1),
'documentKey': {'_id': ObjectId('5b7cfc0172cb100011ddadfb')},
'fullDocument': {'buzName': 'Market',
'v': 29,
'_id': ObjectId('5b7cfc0172cb100011ddadfb'),
'addersValues': [],
'contractType': 'C & I',
'Volume': 54.04637908572,
'VolumeUOM': 'MWh',
'dealType': 'New',
'Companies': [{'Number': '002093834',
'DC': 'flex',
'_id': ObjectId('5b555555cb100011dde53e'),
'FlowEnd': datetime.datetime(2015, 10, 21, 0, 0),
'FlowStart': datetime.datetime(2014, 10, 22, 0, 0),
'profile': 'SERVICE_NORTH',
'rateCode': 'DC'}],
'government': False,
'heatRate': None,
'Sub_companies': [{'Number': '1000002093834',
'DC': 'easy',
'_id': ObjectId('5b555555cb100011dde53e'),
'FlowEnd': datetime.datetime(2015, 10, 21, 0, 0),
'FlowStart': datetime.datetime(2014, 10, 22, 0, 0),
'profile': 'SERVICE_NORTH',
'rateCode': 'DC'}],
'heatRateIndex': None,
'heatRateUOM': None,
'TexasTIndex': None,
'MaxEnd': datetime.datetime(2015, 10, 21, 0, 0),
'originaldate': datetime.datetime(2018, 10, 12, 5, 0),
'originator': 'Luke',
'parentCustomerName': None,
'Directories': ['TEXAS_AdminFeeInc_PT', 'TXLZ_PT'],
'term': 12,
'voluntaryRECPerc': None,
'voluntaryRECs': False,
'westRTIndex': None},
'operationType': 'update'}
【问题讨论】:
-
到目前为止你尝试过什么?另外,这是 mongodb 数据(
ObjectId)吗?Timestamp是什么? -
是的,这是一个 mongoldb 数据,
-
Companies、Directories和Sub_companies是列表,如果有多个项目,您希望如何将它们展平? -
老实说,这是我第一次在 python 中处理规范化 json。这是我在 watchdeal.find() 中尝试输出的内容: print(pd.json_normalize(output,['fullDocument',['Companies','Sub_companies','Directories','dealType']] ,meta=[ 'operationType','term','clusterTime','documnetKey'])
-
问题是,您似乎有三个记录对象:
Companies、Directories和Sub_companies,您需要做出的决定是您希望如何处理数据中的这些列表。如果只有一个记录项,那就容易多了,但如果你只希望在这些记录项中有一个嵌套记录,那就更容易了。
标签: python json pandas normalization