我有一个 json 文件,对 json 和 Pandas 还很陌生。如何根据对话 id 或 csv 数据展平此 JSON。
这里的 Json 数据:https://pastebin.pl/view/64022f08
import json
import pandas as pd
with open('/Users/rp/Downloads/apires.json') as f:
data = json.load(f)
df1 = pd.json_normalize(data)
print(df1)
有人可以帮忙解决这个问题吗?两天想不出来。
答案 0 :(得分:0)
瑞图·帕蒂尔
你的 .json 是 json-dictionary(键,值):
所以为了访问字典中的正确元素,你需要访问这样的元素:
import json
import pandas as pd
data = json.load(open('test.json'))
dfMetadata = pd.DataFrame(data['_metadata'])
dfData =pd.DataFrame(data['conversationHistoryRecords'])
它将这个数据集从 .json 转换为两个数据帧。
为了获得您感兴趣的正确密钥: 在这个结构中,可以检查相关性,并研究这个数据集。
还可以选择访问此行中的特定行:
conversationHistoryRecords = data['conversationHistoryRecords']
campaign = conversationHistoryRecords[0]['campaign']
其中广告系列对于值得探索的数据是可变的(在本例中):
{'campaignEngagementId': '2330596212',
'campaignEngagementName': 'Engagement-123',
'campaignId': '2266771712',
'campaignName': 'Live_Chat_on_your_site',
'goalId': '2266719412',
'goalName': 'Interact with visitors',
'engagementAgentNote': 'agent-note-test-messaging',
'engagementSource': 'WEB_SITE',
'visitorBehaviorId': '2379540212',
'visitorBehaviorName': 'someVisitorBehavior',
'engagementApplicationId': '28879660-84fd-4cd8-a1d7-ba3247bdb252',
'engagementApplicationName': 'Some Mobile App Test',
'engagementApplicationTypeId': '92274cfd-29e7-4d94-a013-0646212d8075',
'engagementApplicationTypeName': 'Mobile App',
'visitorProfileId': '2286779312',
'visitorProfileName': 'All visitors',
'lobId': 2389848512,
'lobName': 'lob_123',
'locationId': '2266779612',
'locationName': 'Entire site',
'profileSystemDefault': True,
'behaviorSystemDefault': False}