I have response from API call as shown below
JavaScript
x
36
36
1
(data = json.loads(response.text)
2
3
{'items': [{'start': '2021-03-21T00:00:00.000',
4
'end': '2021-03-31T00:00:00.000',
5
'location': {'code': None,
6
'position': {'lat': 47.464699, 'lon': 8.54917},
7
'country_code': None},
8
'source': 'geoeditor',
9
'title': 'test 25.03.2021',
10
'body': 'test description',
11
'severity': None,
12
'category': None,
13
'relatedEntities': None,
14
'relevant': None,
15
'raw': {'active': True,
16
'id': 82482150,
17
'layerId': 'disruption_il',
18
'locationType': 'POINT',
19
'name': 'New Location',
20
'changed': '2021-03-25T20:49:51Z',
21
'groupId': None,
22
'identifiers': [{'name': 'ref_id',
23
'value': '9ded7375-bea2-4466-96a9-fd5c42f9a562'}],
24
'properties': {'title': 'test 25.03.2021',
25
'source': 'disruption_news_event',
26
'to_date': '2021-03-31',
27
'relevant': 'true',
28
'from_date': '2021-03-21',
29
'description': 'test description'},
30
'relationships': [{'referenceIdentifierValue': 'ZRH',
31
'relationshipId': 'event_impacts_airport',
32
'referenceLayerId': 'airport_status',
33
'referenceIdentifierName': 'iata_code'}]}}],
34
'totalItems': 1,
35
'errors': []}
36
How can I convert this into proper dataframe. I tried below code its creating the columns are properly. I want each element of the API response as seperate column. For eg. 'position': {'lat': 47.464699, 'lon': 8.54917}
should be created dedicated column for lat and Lon seperately.
JavaScript
1
3
1
df =pd.json_normalize(data)
2
df1= pd.DataFrame(df1)
3
Loop Locations and get values and convert to dataframe
JavaScript
1
14
14
1
location
2
SIN
3
ZRH
4
5
import requests
6
7
headers = {
8
'accept': 'application/json',
9
'Authorization': 'Api-Key',
10
'Content-Type': 'application/json',
11
}
12
13
response = requests.get('https://risk.dev.logindex.com/il/risk/location/SIN', headers=headers)
14
Advertisement
Answer
You can try json_normalize
in loop with append for list of DataFrame
s and then join them by concat
:
JavaScript
1
23
23
1
import requests
2
3
headers = {
4
'accept': 'application/json',
5
'Authorization': 'APIkey',
6
'Content-Type': 'application/json',
7
}
8
9
dfs = []
10
locations = ['SIN','ZRH']
11
for loc in locations:
12
response = requests.get(f'https://risk.dev.logindex.com/il/risk/location/{loc}', headers=headers)
13
data = json.loads(response.text)
14
df = pd.json_normalize(data, 'items', 'totalItems')
15
16
df1 = pd.concat([pd.DataFrame(x) for x in df.pop('raw.identifiers')], keys=df.index).add_prefix('raw.identifiers.')
17
df2 = pd.concat([pd.DataFrame(x) for x in df.pop('raw.relationships')], keys=df.index).add_prefix('raw.relationships.')
18
19
df3 = df.join(df1.join(df2).reset_index(level=1, drop=True))
20
dfs.append(df3)
21
22
df = pd.concat(dfs, ignore_index=True)
23
JavaScript
1
61
61
1
print (df)
2
start end source
3
0 2020-05-11T00:00:00.000 2020-05-18T00:00:00.000 geoeditor
4
1 2020-11-22T00:00:00.000 2020-11-29T00:00:00.000 geoeditor
5
2 2021-03-21T00:00:00.000 2021-03-31T00:00:00.000 geoeditor
6
7
title
8
0 9349576 - Wrecked/stranded (aground)
9
1 9349576 - Collision (involving vessels)
10
2 test 25.03.2021
11
12
body severity category
13
0 Ran aground waters off Batam Indonesia, 11 May None None
14
1 Vessel was involved in collision with Tina I o None None
15
2 test description None None
16
17
relatedEntities relevant location.code location.position.lat
18
0 None None None 1.350190
19
1 None None None 1.350190
20
2 None None None 47.464699
21
22
location.position.lon location.country_code raw.active raw.id
23
0 103.994003 None True 73459846
24
1 103.994003 None True 73478162
25
2 8.549170 None True 82482150
26
27
raw.layerId raw.locationType raw.name
28
0 disruption_il POINT 9349576 - Wrecked/stranded (aground)
29
1 disruption_il POINT 9349576 - Collision (involving vessels)
30
2 disruption_il POINT New Location
31
32
raw.changed raw.groupId raw.properties.title
33
0 2021-03-02T11:31:13Z None 9349576 - Wrecked/stranded (aground)
34
1 2021-03-04T21:01:12Z None 9349576 - Collision (involving vessels)
35
2 2021-03-25T20:49:51Z None test 25.03.2021
36
37
raw.properties.source raw.properties.to_date raw.properties.relevant
38
0 disruption_vessel_issues 2020-05-18 true
39
1 disruption_vessel_issues 2020-11-29 true
40
2 disruption_news_event 2021-03-31 true
41
42
raw.properties.from_date raw.properties.description
43
0 2020-05-11 Ran aground waters off Batam Indonesia, 11 May
44
1 2020-11-22 Vessel was involved in collision with Tina I o
45
2 2021-03-21 test description
46
47
totalItems raw.identifiers.name raw.identifiers.value
48
0 2 ref_id disruption_vessel_issues:299346
49
1 2 ref_id disruption_vessel_issues:307022
50
2 1 ref_id 9ded7375-bea2-4466-96a9-fd5c42f9a562
51
52
raw.relationships.referenceIdentifierValue raw.relationships.relationshipId
53
0 disruption_vessel_issues:299346 event_source_link
54
1 disruption_vessel_issues:307022 event_source_link
55
2 ZRH event_impacts_airport
56
57
raw.relationships.referenceLayerId raw.relationships.referenceIdentifierName
58
0 disruption_kn_disruption ref_id
59
1 disruption_kn_disruption ref_id
60
2 airport_status iata_code
61