Feb-05-2021, 02:43 PM
(Feb-02-2021, 10:02 PM)eddywinch82 Wrote: Hi firaki12345,
Here is a more cleaned up output version, of the previous Code :-
I hope you like it ))
import json import pandas as pd import re with open("walmart json file.txt") as f: data = json.load(f) walmart = data["items"] wdf = pd.DataFrame(walmart,columns=["productId","primaryOffer"]) print(wdf.loc[0,"primaryOffer"]) pd.set_option('display.max_colwidth', None) wdf['primaryOffer'] = wdf['primaryOffer'].astype(str) wdf['primaryOffer'] = wdf.primaryOffer.str.replace("'", '') wdf['primaryOffer'] = wdf.primaryOffer.str.replace(",", '') wdf['primaryOffer'] = wdf.primaryOffer.str.replace("{", '') wdf['offerId'] = wdf.primaryOffer.str.extract('.*offerId:\s?(\w+)', expand = False) wdf['minPrice(USD)'] = wdf.primaryOffer.str.extract('.*minPrice:\s?(\d+[.]?\d*)', expand = False) wdf['maxPrice(USD)'] = wdf.primaryOffer.str.extract('.*maxPrice:\s?(\d+[.]?\d*)', expand = False) wdf['offerPrice(USD)'] = wdf.primaryOffer.str.extract('.*offerPrice:\s?(\d+[.]?\d*)', expand = False) wdf=wdf.fillna('--') wdf.drop('primaryOffer', axis=1, inplace=True) wdfRegards
Eddie Winch
hi very very sorry for the late reply, but as i said i found an easier solution by using json_normalize
as it turns all jumbled up data into columns. so it becomes easy to use in pandas :)