2 回答
TA贡献1820条经验 获得超10个赞
pandas几乎总是在与表交互时使用。它可以解析字典
In [0]: import pandas
In [1]: from pandas.io.json import json_normalize
In [2]: d = {'duration': 202.0,
...: 'session_info':
...: {'activation_uuid': 'ab90d941-df9d-42c5-af81-069eb4f71515',
...: 'launch_uuid': '11101c41-2d79-42cc-bf6d-37be46802fc8'},
...: 'timestamp': '2019-01-18T11:11:26.135Z',
...: 'source_page_view_reference':
...: {'page_uuid': '1bede017-7b77-461d-82ef-a6bbcfdae4d7',
...: 'page_id': '/group/More',
...: 'page_name': 'More',
...: 'view_uuid': '9580f3c5-1116-432a-83bc-9d0b5337f661',
...: 'page_type': 'Native'},
...: 'analytics_sdk':
...: {'component_id': 'datasdk',
...: 'component_version': '1.0.52'},
...: 'treatment_id': 'mockTreat',
...: 'client_event_id': '2b3cd878-6932-410b-b1ad-bc40ae888fdc',
...: 'campaign_id': 'mockCamp'}
In [4]: json_normalize(d)
Out[4]:
analytics_sdk.component_id analytics_sdk.component_version campaign_id client_event_id duration ... source_page_view_reference.page_type source_page_view_reference.page_uuid source_page_view_reference.view_uuid timestamp treatment_id
0 datasdk 1.0.52 mockCamp 2b3cd878-6932-410b-b1ad-bc40ae888fdc 202.0 ... Native 1bede017-7b77-461d-82ef-a6bbcfdae4d7 9580f3c5-1116-432a-83bc-9d0b5337f661 2019-01-18T11:11:26.135Z mockTreat
[1 rows x 14 columns]
要将 JSON 字符串加载到字典中,请使用 json.loads
或使用 pandas.read_json
TA贡献2065条经验 获得超13个赞
您也可以通过以下方式进行,这类似于 pandas 内部所做的事情。
import json
jsondata='''{
"duration": 202.0,
"session_info": {
"activation_uuid": "ab90d941-df9d-42c5-af81-069eb4f71515",
"launch_uuid": "11101c41-2d79-42cc-bf6d-37be46802fc8"
},
"timestamp": "2019-01-18T11:11:26.135Z",
"source_page_view_reference": {
"page_uuid": "1bede017-7b77-461d-82ef-a6bbcfdae4d7",
"page_id": "/group/More",
"page_name": "More",
"view_uuid": "9580f3c5-1116-432a-83bc-9d0b5337f661",
"page_type": "Native"
},
"analytics_sdk": {
"component_id": "datasdk",
"component_version": "1.0.52"
},
"treatment_id": "mockTreat",
"client_event_id": "2b3cd878-6932-410b-b1ad-bc40ae888fdc",
"campaign_id": "mockCamp"
}'''
data=json.loads(jsondata)
table=[[],[]]
def dictList(d, column_name=''):
for k, v in d.items():
if isinstance(v, dict):
dictList(v, column_name=k)
continue
if column_name:
column_name+='.'
column_name +=k
table[0].append(column_name)
table[1].append(v)
dictList(data)
for row in table:
print (row)
- 2 回答
- 0 关注
- 169 浏览
添加回答
举报