Darshan Jadav
Darshan Jadav

Reputation: 484

Pandas Match row to column values

I have a JSON output which I am trying to get in excel.

enter image description here

What I am trying to do is match the WEIGHT as column Header. I could get this output using some loops.

What I am trying to get is have all the Weights as the first column header and if it has values paste it in else NaN.

Desired Output: enter image description here

page = requests.get(mainurl)

data = json.loads(page.text)
for i in data['categories']:
        for j in i['items']:
                if a == 1:  # so changes and appends keys per category (highlighted)
                    a=2 #  so not true in this loop
                    s=tuple(j['prices'].keys())
                    ws.append(s)
                PVAL=list(j['prices'].values())

                ws.append(PVAL)# append the value 
        a=1 # makes true next category

p= []
for i in price: # I know this is absolute madness but dicts were getting sorted 
    i = str(i).replace("'",'').replace('{','').replace('}','')# get price values
    p.append(i)
###apppend in excel

Note : As you can tell by the above code, I am a complete Beginner. And the above code could have been pretty with 2-3 lines of Pandas :( I am now tinkering with Pandas to do it since I think it will be faster and better.

JsonOutput

MAJOR EDIT:

So I didn't have much time so I did this:

   for i in data['categories']:
        for j in i['items']:
            PVAL=j['prices']
            try:
                ounce = PVAL['ounce']
            except:
                ounce = 'NaN'
            try:
                gram = PVAL['gram']
            except:gram = 'NaN'
            try:
                twograms = PVAL['two_grams']
            except:twograms='NaN'
            try:
                quarter=PVAL['quarter']
            except:quarter='NaN'
            try:

                eighth=PVAL['eighth']
            except:eighth='NaN'
            try:

                halfO=PVAL['half_ounce']
            except:halfO='NaN'
            try:
                unit = PVAL['unit']
            except:unit='NaN'
            try:
                halfgram = PVAL['half_gram']
            except:halfgram='NaN'

            name= j['name']
            cat = j['category_name']
            listname = j['listing_name']

            namel.append(name)
            catl.append(cat)
            listnamel.append(listname)

            halfOl.append(halfO)
            halfgraml.append(halfgram)
            unitl.append(unit)
            eighthl.append(eighth)
            twogramsl.append(twograms)
            quarterl.append(quarter)
            ouncel.append(ounce)
            graml.append(gram)

Then these lists are appended in Excel. I know it is not Pythonic but I am still trying to findout a good way to do it in Pandas.

Upvotes: 3

Views: 258

Answers (1)

oim
oim

Reputation: 1151

as my rep is still low, cannot post any comment yet, thus will just post it here and will edit this if further clarifications are provided.

I don't see any WEIGHTs in the desired output. If I understand the json file correctly, you are iterating prices given a weight unit. Is the expected output to loop over each item and iterate over prices per weight unit. Put NaN if weight unit is not available. Is there a list of possible weight units?

Pandas also has read_json function and thus can directly load this to a Pandas dataframe.

-- edited ---

Apologies for the delay. Please see below answer

import pandas as pd
import json
from cytoolz.dicttoolz import merge

#replace below with your json loader
with open('sample.json') as json_dta:
    dict_dta = json.load(json_dta)

list_columns = ['id', 'name', 'category_name', 'ounce', 'gram', 'two_grams', 'quarter', 'eighth','half_ounce','unit','half_gram']
df = pd.io.json.json_normalize(dict_dta, ['categories', ['items']]).pipe(lambda x: x.drop('prices', 1).join(x.prices.apply(lambda y: pd.Series(merge(y)))))[list_columns]

Above will result to:

          id                                     name category_name  ounce  gram  two_grams  quarter  eighth  half_ounce  unit  half_gram
0   10501503                                    Recon        Indica   99.0   9.0        0.0     40.0    25.0        70.0   NaN        NaN
1   11614583                                Kush Dawg        Indica   99.0   9.0        0.0     40.0    25.0        70.0   NaN        NaN
2    8602219                                  OG Kush        Indica   99.0   9.0        0.0     40.0    25.0        70.0   NaN        NaN
3   11448858                        Poison OG Outdoor        Sativa   69.0   9.0        0.0     40.0    25.0        50.0   NaN        NaN
4   11731126                      SunBurn 2.0 Outdoor        Sativa   69.0   0.0        0.0      0.0     0.0         0.0   NaN        NaN
5    6412418                                Poison OG        Sativa   99.0   9.0       18.0     40.0    25.0        70.0   NaN        NaN
6    8982466                              Sativa Trim        Sativa   30.0   0.0        0.0      0.0     0.0        15.0   NaN        NaN
7   11545434                       Chupacabra Outdoor        Hybrid   69.0   9.0        0.0     40.0    25.0        50.0   NaN        NaN
8   11458944      Platinum Girl Scout Cookies Outdoor        Hybrid   69.0   9.0        0.0     40.0    25.0        50.0   NaN        NaN
9   11296163                                Bubblegum        Hybrid   99.0   9.0        0.0     40.0    25.0        70.0   NaN        NaN
10  11614623                                       C4        Hybrid   99.0   9.0        0.0     40.0    25.0        70.0   NaN        NaN
11  11333124                        Chem Dawg Outdoor        Hybrid   69.0   9.0        0.0     40.0    25.0        50.0   NaN        NaN
12  11458988                               Candy Kush        Hybrid   99.0   9.0        0.0     40.0    25.0        70.0   NaN        NaN
13  10501592                       Candy Kush Outdoor        Hybrid   69.0   9.0        0.0     40.0    25.0        50.0   NaN        NaN
14   9123804                     ZOOTROCKS LemonGrass        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
15   9412336                     Cherry Limeade 100mg        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
16   4970503           Peanut Budda Buddha, 100mg REC        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
17   9412238    Golden Strawberry Puck 100mg REC - CO        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
18   9412232               Cherry Puck 100mg REC - CO        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
19   9412228       Assorted Sour Pucks 100mg REC - CO        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
20   6454686     Assorted Fruity Pucks 100mg REC - CO        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
21   9412295  Sour Gummies Sativa 100mg, Recreational        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
22   7494303                             Cheeba Chews        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
23   9411974                Mile High Mint, 100mg REC        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
24   9411972                       Boulder Bar, 100mg        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
25   9412286  Sour Gummies Indica 100mg, Recreational        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
26   9412242              Watermelon Puck 100mg - REC        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
27  10066310                   Coffee & Doughnuts        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
28  10065124                         Wildflower Honey        Edible    NaN   NaN        NaN      NaN     NaN         NaN  24.0        NaN
29  10064962                             Clover Honey        Edible    NaN   NaN        NaN      NaN     NaN         NaN  24.0        NaN
30   9412290   Sour Gummies Peach 100mg, Recreational        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
31   5926966                            Stratos 100mg        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
32  10066271                          Salt & Nibs        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
33  10065225                       Yampa Valley Honey        Edible    NaN   NaN        NaN      NaN     NaN         NaN  24.0        NaN
34   9412873                  Fruit Punch Mints 100mg        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
35   9412251  Sour Gummies Hybrid 100mg, Recreational        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
36   9412922          Dutch Girl Carmel Waffle, 100mg        Edible    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
37   6790292                    Hybrid Distillate Jar   Concentrate    NaN  36.0        0.0      NaN     NaN         NaN   NaN        0.0
38   6379060                         Hybrid Cartridge   Concentrate    NaN  25.0        0.0      NaN     NaN         NaN   NaN       18.0
39   9009149                 Pure Cannabis Oil Hybrid   Concentrate    NaN  25.0        0.0      NaN     NaN         NaN   NaN        0.0
40   9400145                 Pure Cannabis Oil Sativa   Concentrate    NaN  25.0        0.0      NaN     NaN         NaN   NaN        0.0
41   9409961                         Sativa Cartridge   Concentrate    NaN  25.0        0.0      NaN     NaN         NaN   NaN       18.0
42   9400121                 Pure Cannabis Oil Indica   Concentrate    NaN  25.0        0.0      NaN     NaN         NaN   NaN        0.0
43   9409954                         Indica Cartridge   Concentrate    NaN  25.0        0.0      NaN     NaN         NaN   NaN       18.0
44   9400467                    Indica Distillate Jar   Concentrate    NaN  36.0        0.0      NaN     NaN         NaN   NaN        0.0
45   9691836                       PWO Wax by Mahatma   Concentrate    NaN  25.0        0.0      NaN     NaN         NaN   NaN        0.0
46   9409970                    Sativa Distillate Jar   Concentrate    NaN  36.0        0.0      NaN     NaN         NaN   NaN        0.0
47   6134675                                    Bongs          Gear    NaN   NaN        NaN      NaN     NaN         NaN  40.0        NaN
48   5993354                        Small Glass Pipes          Gear    NaN   NaN        NaN      NaN     NaN         NaN  10.0        NaN
49   4393434                        Large Glass Pipes          Gear    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
50   5941409                   Pain Relief Salve, 2oz      Topicals    NaN   NaN        NaN      NaN     NaN         NaN  26.0        NaN
51   8768835                           THC Pain Stick      Topicals    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
52   6370279       FORIA Pleasure (30ml) Spray Bottle      Topicals    NaN   NaN        NaN      NaN     NaN         NaN  55.0        NaN
53   8911546                                Bath Soak      Topicals    NaN   NaN        NaN      NaN     NaN         NaN  20.0        NaN
54   9123854      FORIA Relief (2-pack) Suppositories      Topicals    NaN   NaN        NaN      NaN     NaN         NaN  24.0        NaN
55   4187102          1 Gram Strain Specific-Prerolls       Preroll    NaN   NaN        NaN      NaN     NaN         NaN   9.0        NaN

Upvotes: 3

Related Questions