Blue Moon
Blue Moon

Reputation: 4651

How to append json files to a dataframe?

I have a large pandas data frame with over 300 columns. One of these columns is the product id. Each product is linked to a json file through the url:

"https://cws01.worldstores.co.uk/api/product.php?product_sku="+id

what I need to do is to load the information stored in json files and append to the row with the same product id all those values whose attribute key match a column in the dataset and fill blank those without a match

json file attributes are fewer than columns in the pandas data frame so the dataset will be somehow sparse.

I don't know how to get started. I wish I could do it in python. Any idea on how the tasks breakdown would be? many thanks

Upvotes: 0

Views: 1484

Answers (1)

MattH
MattH

Reputation: 38247

Something alone the lines of:

import requests

def get_product(id):
    r = requests.get('https://cws01.worldstores.co.uk/api/product.php',params=dict(product_sku=id))
    return r.json()

for row in dataframe:
  remote_data = get_product(row['product_id'])
  for key in row.keys():
    if key in remote_data:
      row[key] = remote_data[key]

Upvotes: 1

Related Questions