Home > Software design >  Pandas How to combine multiple data columns and output as dictionary
Pandas How to combine multiple data columns and output as dictionary

Time:05-18

I am working on csv file. The csv table file structure is as

brands models 2021_price 2020_price
chevrolet Traverse 320000 24000
chevrolet Equinox 23000 18000
chevrolet Trailblazer 13000 14000

this is what I tried on my own

json_dict = {}
for index,row in df.iterrows():
    data=(
        {row[0]:{
        ''.join(str(row[1])):
            {
             "2021":' '.join(str(row[2]).split()),
             '2020':' '.join(str(row[3]).split()),
            }
        }
        }
    )
    json_dict.update(data)

I got this as an output

 {
      "chevrolet":{
         "Traverse":{
            "2021":"320000",
            "2020":"24000",
         },
      
         "chevrolet":{
         "Equinox":{
            "2021":"23000",
            "2020":"18000",
           
         } 
   }

but expected dictionary is as

{
      "chevrolet":{
         "Traverse":{
            "2021":"320000",
            "2020":"24000"
         },
      
         "Equinox":{
            "2021":"23000",
            "2020":"18000"
         } 
   }

this is sample of file

NISSAN    Patrol Platinum City  1,260,000,000.00 UZS   Nan      
    NISSAN       Qasgqai        315,000,000.00 UZS    315,000,
    NISSAN       X-Trail        367,500,000.00 UZS     Nan

CodePudding user response:

If I understand you correctly, you want to group by "brands" and then create a dictionary:

out = {}
for b, g in df.groupby("brands"):
    out[b] = {
        row["models"]: {
            "2020": row["2020_price"],
            "2021": row["2021_price"],
        }
        for _, row in g.iterrows()
    }

print(out)

Prints:

{
    "chevrolet": {
        "Traverse": {"2020": 24000, "2021": 320000},
        "Equinox": {"2020": 18000, "2021": 23000},
        "Trailblazer": {"2020": 14000, "2021": 13000},
    }
}
  • Related