Max
Max

Reputation: 471

Get JSON response for each df row

Imagine you have the following df:

d = {'KvK': [72941014, 76912027, 75090058], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]}
df = pd.DataFrame(data=d)
df

      KvK   line am   ExclBTW   APIOutput
0   72941014    0.00    0.50    https://api.kvk.nl/api/v2/search/companies?use...
1   76912027    0.05    0.18    https://api.kvk.nl/api/v2/search/companies?use...
2   75090058    0.05    0.05    https://api.kvk.nl/api/v2/search/companies?use...

Now I added the APIOutput column:

df['APIOutput'] = df['KvK'].apply(lambda x: f"https://api.kvk.nl/api/v2/search/companies?user_key=l7xxd3eea0c598a645a7fa69dbb&q={x}")

I want to get all the JSON responses from the APIOutput into one column, apioutput1.

I've tried the following code;

df['apioutput1'] = requests.get.apply(lambda x: f"df['APIOutput']{x}", verify=False)

However the above does not work..

Desired output:

       KvK     lineam  ExclBTW       APIOutput                                     apioutput1
0   72941014    0.00    0.50    https://api.kvk.nl/api/v2/search/companies?use...   JSON
1   76912027    0.05    0.18    https://api.kvk.nl/api/v2/search/companies?use...   JSON
2   75090058    0.05    0.05    https://api.kvk.nl/api/v2/search/companies?use...   JSON

how to achieve the above?

Please help

Upvotes: 0

Views: 63

Answers (2)

Shubham Periwal
Shubham Periwal

Reputation: 2248

Use .apply to call requests.get on every row

df['apioutput1'] = df['APIOutput'].apply(lambda x: requests.get(x, verify=False) )

Upvotes: 0

Barmar
Barmar

Reputation: 780974

Call apply on the series, just like you did when filling in the APIOutput column.

df['apioutput1'] = df['APIOutput'].apply(lambda url: requests.get(url, verify=False))

Upvotes: 2

Related Questions