我正在尝试将json文件转换为Excel并进行修改。在对json进行规范化并尝试添加列之后,我会收到一个错误消息,指出轴0的索引20超出了大小19。然而,当我从JSON中规范化3个东西时,我不会收到这个错误,但是当我规范化只有2个东西时,我会收到一个错误。
以下是我的代码:
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()