Python: How to set up bounds for index

Viewed 45

I'm trying to convert json file to excel and modify it. After normalizing the json and try to add columns I get an error saying index 20 is out of bounds for axis 0 with size 19. However, when I normalize 3 things from JSON I don't get this error but when I normalize just 2 things I get an error.

Here's my code

def get_data(link :str):

  resp = requests.get(link) #reading link
  txt = resp.json()
  data = pd.DataFrame(txt['products']) #data
  return txt

def main():

  #get json data from link
  json = get_data(link = 'https://0f91c5da166bc1b5a70cce01e1f0370c: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@shopeclat.myshopify.com/admin/api/2022-07/products.json')

  v = pd.json_normalize(json['products'], record_path =['variants'],meta=['id','title','body_html', 'vendor','product_type','created_at','updated_at','status','image','tags'],record_prefix='varients_')


  i = pd.json_normalize(json['products'], record_path =['images'],meta=['id','title','body_html', 'vendor','product_type','created_at','updated_at','status','image','tags'],record_prefix='images_')


  #merging all three dataset on id
  df = [v,i]
  final_df = reduce(lambda  left,right: pd.merge(left,right,on=['id'],
                                            how='outer'), df)

  print("Exporting csv files ....")
  final_df.to_csv('Bound.csv',index = False)
  
if __name__ == '__main__':
    main()
1 Answers

Maybe .explode() is what you want:

import requests
import pandas as pd

url = "https://0f91c5da166bc1b5a70cce01e1f0370c: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@shopeclat.myshopify.com/admin/api/2022-07/products.json"


df = (
    pd.DataFrame(requests.get(url).json()["products"])
    .explode("variants")
    .explode("options")
    .explode("images")
)

df = pd.concat(
    [
        df,
        df.pop("variants").apply(pd.Series).add_prefix("v_"),
        df.pop("options").apply(pd.Series).add_prefix("o_"),
        df.pop("images").apply(pd.Series).add_prefix("imgs_"),
    ],
    axis=1,
)

df.to_csv("out.csv", index=False)

Creates out.csv (screenshot from Libre Office):

enter image description here

Related