gpt4 book ai didi

python - 如何在python中将具有多个级别的API响应转换为数据帧

转载 作者:行者123 更新时间:2023-12-04 07:46:03 25 4
gpt4 key购买 nike

我有 API 调用的响应,如下所示

(data = json.loads(response.text)

{'items': [{'start': '2021-03-21T00:00:00.000',
'end': '2021-03-31T00:00:00.000',
'location': {'code': None,
'position': {'lat': 47.464699, 'lon': 8.54917},
'country_code': None},
'source': 'geoeditor',
'title': 'test 25.03.2021',
'body': 'test description',
'severity': None,
'category': None,
'relatedEntities': None,
'relevant': None,
'raw': {'active': True,
'id': 82482150,
'layerId': 'disruption_il',
'locationType': 'POINT',
'name': 'New Location',
'changed': '2021-03-25T20:49:51Z',
'groupId': None,
'identifiers': [{'name': 'ref_id',
'value': '9ded7375-bea2-4466-96a9-fd5c42f9a562'}],
'properties': {'title': 'test 25.03.2021',
'source': 'disruption_news_event',
'to_date': '2021-03-31',
'relevant': 'true',
'from_date': '2021-03-21',
'description': 'test description'},
'relationships': [{'referenceIdentifierValue': 'ZRH',
'relationshipId': 'event_impacts_airport',
'referenceLayerId': 'airport_status',
'referenceIdentifierName': 'iata_code'}]}}],
'totalItems': 1,
'errors': []}
如何将其转换为正确的数据帧。我试过下面的代码,它创建的列是正确的。我希望 API 响应的每个元素作为单独的列。例如。 'position': {'lat': 47.464699, 'lon': 8.54917}应分别为 lat 和 Lon 创建专用列。
df =pd.json_normalize(data)
df1= pd.DataFrame(df1)
循环位置并获取值并转换为数据帧
location
SIN
ZRH

import requests

headers = {
'accept': 'application/json',
'Authorization': 'Api-Key',
'Content-Type': 'application/json',
}

response = requests.get('https://risk.dev.logindex.com/il/risk/location/SIN', headers=headers)

最佳答案

你可以试试 json_normalize 在循环中附加 DataFrame 的列表s 然后通过 concat 加入他们:

import requests

headers = {
'accept': 'application/json',
'Authorization': 'APIkey',
'Content-Type': 'application/json',
}

dfs = []
locations = ['SIN','ZRH']
for loc in locations:
response = requests.get(f'https://risk.dev.logindex.com/il/risk/location/{loc}', headers=headers)
data = json.loads(response.text)
df = pd.json_normalize(data, 'items', 'totalItems')

df1 = pd.concat([pd.DataFrame(x) for x in df.pop('raw.identifiers')], keys=df.index).add_prefix('raw.identifiers.')
df2 = pd.concat([pd.DataFrame(x) for x in df.pop('raw.relationships')], keys=df.index).add_prefix('raw.relationships.')

df3 = df.join(df1.join(df2).reset_index(level=1, drop=True))
dfs.append(df3)

df = pd.concat(dfs, ignore_index=True)
print (df)
start end source \
0 2020-05-11T00:00:00.000 2020-05-18T00:00:00.000 geoeditor
1 2020-11-22T00:00:00.000 2020-11-29T00:00:00.000 geoeditor
2 2021-03-21T00:00:00.000 2021-03-31T00:00:00.000 geoeditor

title \
0 9349576 - Wrecked/stranded (aground)
1 9349576 - Collision (involving vessels)
2 test 25.03.2021

body severity category \
0 Ran aground waters off Batam Indonesia, 11 May... None None
1 Vessel was involved in collision with Tina I o... None None
2 test description None None

relatedEntities relevant location.code location.position.lat \
0 None None None 1.350190
1 None None None 1.350190
2 None None None 47.464699

location.position.lon location.country_code raw.active raw.id \
0 103.994003 None True 73459846
1 103.994003 None True 73478162
2 8.549170 None True 82482150

raw.layerId raw.locationType raw.name \
0 disruption_il POINT 9349576 - Wrecked/stranded (aground)
1 disruption_il POINT 9349576 - Collision (involving vessels)
2 disruption_il POINT New Location

raw.changed raw.groupId raw.properties.title \
0 2021-03-02T11:31:13Z None 9349576 - Wrecked/stranded (aground)
1 2021-03-04T21:01:12Z None 9349576 - Collision (involving vessels)
2 2021-03-25T20:49:51Z None test 25.03.2021

raw.properties.source raw.properties.to_date raw.properties.relevant \
0 disruption_vessel_issues 2020-05-18 true
1 disruption_vessel_issues 2020-11-29 true
2 disruption_news_event 2021-03-31 true

raw.properties.from_date raw.properties.description \
0 2020-05-11 Ran aground waters off Batam Indonesia, 11 May...
1 2020-11-22 Vessel was involved in collision with Tina I o...
2 2021-03-21 test description

totalItems raw.identifiers.name raw.identifiers.value \
0 2 ref_id disruption_vessel_issues:299346
1 2 ref_id disruption_vessel_issues:307022
2 1 ref_id 9ded7375-bea2-4466-96a9-fd5c42f9a562

raw.relationships.referenceIdentifierValue raw.relationships.relationshipId \
0 disruption_vessel_issues:299346 event_source_link
1 disruption_vessel_issues:307022 event_source_link
2 ZRH event_impacts_airport

raw.relationships.referenceLayerId raw.relationships.referenceIdentifierName
0 disruption_kn_disruption ref_id
1 disruption_kn_disruption ref_id
2 airport_status iata_code

关于python - 如何在python中将具有多个级别的API响应转换为数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67189378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com