title: How to convert the following dataframe/dataset to nested json as desired format?
body: <p>The data set are follows:</p>
  <pre><code>id,date,diastolic,systolic,drugs_name,drugs_type,drugs_unit,drugs_dosage,drugs_time
  
  10001,2020-09-15,70,120,paracetamol,tablet,1,twice a day,after meal
  10001,2020-09-15,70,120,ursocol,tablet,1,twice a day,before meal
  10001,2020-09-15,70,120,rectacare,ointment,1,use once a day,any time
  10001,2020-11-11,85,135,metronidazole,tablet,1,trice a day,after meal
  10001,2020-11-11,85,135,antacid,tablet,1,twice a day,before meal
  </code></pre>
  <p>My desire output is as follow:</p>
  <pre><code> [
      {
          &quot;date&quot;: &quot;2020-09-15&quot;,
          **&quot;bp&quot;:{&quot;diastolic&quot;: 70, &quot;systolic&quot;: 120},**
          &quot;drugs&quot;: [
              {
                  &quot;drugs_name&quot;: &quot;paracetamol&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;twice a day&quot;,
                  &quot;drugs_time&quot;: &quot;after meal&quot;
              },
              {
                  &quot;drugs_name&quot;: &quot;ursocol&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;twice a day&quot;,
                  &quot;drugs_time&quot;: &quot;before meal&quot;
              },
              {
                  &quot;drugs_name&quot;: &quot;rectacare&quot;,
                  &quot;drugs_type&quot;: &quot;ointment&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;use once a day&quot;,
                  &quot;drugs_time&quot;: &quot;any time&quot;
              }
          ]
      },
      {
          &quot;date&quot;: &quot;2020-11-11&quot;,
          **&quot;bp&quot;:{&quot;diastolic&quot;: 85,&quot;systolic&quot;: 135}**,
          &quot;drugs&quot;: [
              {
                  &quot;drugs_name&quot;: &quot;metronidazole&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;trice a day&quot;,
                  &quot;drugs_time&quot;: &quot;after meal&quot;
              },
              {
                  &quot;drugs_name&quot;: &quot;antacid&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;twice a day&quot;,
                  &quot;drugs_time&quot;: &quot;before meal&quot;
              }
          ]
      }
  ]
  </code></pre>
  <p>I wrote a code stated here:</p>
  <pre><code>df['date']=df.date.apply(lambda x: x.strftime(format_str))
  json_ofmysqlDataframe = (
            sql_df.groupby(['date','diastolic','systolic'])
           .apply(lambda x: x[['drugs_name', 'drugs_type', 'drugs_unit', 'drugs_dosage', 'drugs_time']].to_dict('r'))
           .reset_index()
           .rename(columns={0:'drugs'})                         
           .to_json(orient='records')             
           )  
  loaded_json=json.loads(json_ofmysqlDataframe)
  json_convert=json.dumps(loaded_json,indent=4, sort_keys=False)
  print(&quot;\n\n\n&quot;,json_convert,&quot;\n\n&quot;)
  </code></pre>
  <p>The output was as follows:</p>
  <pre><code>[
      {
          &quot;date&quot;: &quot;2020-09-15&quot;,
          **&quot;diastolic&quot;: 70,
          &quot;systolic&quot;: 120,**
          &quot;drugs&quot;: [
              {
                  &quot;drugs_name&quot;: &quot;paracetamol&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;twice a day&quot;,
                  &quot;drugs_time&quot;: &quot;after meal&quot;
              },
              {
                  &quot;drugs_name&quot;: &quot;ursocol&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;twice a day&quot;,
                  &quot;drugs_time&quot;: &quot;before meal&quot;
              },
              {
                  &quot;drugs_name&quot;: &quot;rectacare&quot;,
                  &quot;drugs_type&quot;: &quot;ointment&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;use once a day&quot;,
                  &quot;drugs_time&quot;: &quot;any time&quot;
              }
          ]
      },
      {
          &quot;date&quot;: &quot;2020-11-11&quot;,
          **&quot;diastolic&quot;: 85,
          &quot;systolic&quot;: 135,**
          &quot;drugs&quot;: [
              {
                  &quot;drugs_name&quot;: &quot;metronidazol&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;trice a day&quot;,
                  &quot;drugs_time&quot;: &quot;after meal&quot;
              },
              {
                  &quot;drugs_name&quot;: &quot;antacid&quot;,
                  &quot;drugs_type&quot;: &quot;tablet&quot;,
                  &quot;drugs_unit&quot;: &quot;1&quot;,
                  &quot;drugs_dosage&quot;: &quot;twice a day&quot;,
                  &quot;drugs_time&quot;: &quot;before meal&quot;
              }
          ]
      }
  ] 
  </code></pre>
  <p>Would please help me to get &quot;bp&quot; in desired format?</p>