Reputation: 423
I have an initial df that looks like:
id | title | urls |
---|---|---|
1 | title1 | www.url1.com |
2 | title2 | www.url2.com |
I would like to use apply
or some loop that can take each row and after an api call unpack a json blob into new column data for each row...
for r in df:
json_blob = make_api_call(r.urls)
# take json blob and put into df?
pd.read_json(json_blob)
# add to row r back in the original df?
Such that given the api data ->
{
"validThrough": "2022-10-16",
"description": "this and that",
"Location": {
"geo": {
"longitude": "-73.962547",
"latitude": "40.687089",
"@type": "GeoCoordinates"
}
}
df after everything ->
title | urls | validThrough | description | Location.geo.longitude | Location.geo.latitude | Location.geo.@type | |
---|---|---|---|---|---|---|---|
0 | title1 | www.url1.com | 2022-10-16 | this is a description | -73.962547 | 40.687089 | GeoCoordinates |
n | ... |
Upvotes: 0
Views: 152
Reputation: 423
Solved this with
def get_api_data(url):
json_data = make_req(url)
# returns a series that apply can use
return pd.json_normalize(job_info).squeeze()
df1 = df.apply(lambda x: get_api_data(x['url']), axis=1)
df = pd.concat([df, df1], axis=1)
Upvotes: 1