0

I am working on a project where i need to reshape the data in particular Format. I have transformed successfully in Excel but how we can transformed this Data with pandas because in Excel we have the limitation of rows.

Territory ID    Patient Id  Doctor Id   Clinic Count    Attribute          Value
43                172        6021          Clinic1      Service Date      06/22/2017 
43                172        6021          Clinic1      Product               A
43                172        6021          Clinic1      Qty                   1
43                172        6021          Clinic1      Amount                80.63
43                172        6021          Clinic1      Age                   1
43                172        6021          Clinic1      Days Last Clinic      0
43                187        5658          Clinic2      Service Date       06/30/2017 
43                187        5658          Clinic2      Product               B
43                187        5658          Clinic2      Qty                   2
43                187        5658          Clinic2      Amount                52.48
43                187        5658          Clinic2      Age                   9y1m
43                187        5658          Clinic2      Days Last Clinic      23

This has to be reshape in this format:

TerritoryID PatientId   DoctorId    Clinic_Count    Service_Date    Product    Qty    Amount   Age    Days Last Clinic   
    
43            172        6021          Clinic1      06/22/2017       A           1     80.63    1          0
43            187        5658          Clinic2      06/30/2017       B           2     52.48    9y1m       23
rpanai
  • 12,515
  • 2
  • 42
  • 64

1 Answers1

0

Try this:

import pandas as pd
df = pd.DataFrame({...}) # Your DataFrame

New_Data = {x:[] for x in list(df.columns)+list(df['Attribute'])}

for x in list(df.columns):
    for j, i in enumerate(list(df[x])):
        if not list(df['Clinic Count'])[j] in New_Data['Clinic Count']:
            New_Data[x].append(i)

New_Data.pop('Attribute')
New_Data.pop('Value')

for x, i in zip(list(df['Attribute']), list(df['Value'])):
    New_Data[x].append(i)