0

dataframe I have one example dataframe above, I was wondering is it possible that it can be converted into something like below. If yes, do enlighten me. Thanks in advance!

expected_output

{
 "items":[{"outlet":"Outlet A","count":"11","amount":"22"},
      {"outlet":"Outlet B","count":"22","amount":"33"},
      {"outlet":"Outlet C","count":"33","amount":"44"}],
 "merchant":"Merchant A"
}

1 Answers1

0

hey I wrote this and it works, you can modify it as you like although I'm sure it's not fully optimized.

datdict={'items':[],'merchant':[]}
for i,a,b,c in zip(df.merchant,df.outlet,df.counted,df.amount):
    datdict['items'].append(dict(outlet=a,counted=b,amount=c))
    if i not in datdict['merchant']:
        datdict['merchant'].append(i)
Anthony1223
  • 351
  • 2
  • 7