Pandas matches rows to column values
I
have a JSON output and I want to get it in excel.
What I want to do is match WEIGHT as a column header.
I can get this output using some loops.
What I want is to have all the weights as the first column header, and if it has values, paste them into other NaN.
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 see from the above code, I am a complete beginner. The above code can be written well with 2-3 lines Pandas 🙁
I’m tinkering with Pandas to do this now because I think it’s going to be faster and better.
Main Editor:
So I didn’t have much time so I did it :
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 attach these lists to Excel.
I know it’s not Pythonic, but I’m still trying to find a good way to implement it in Pindas.
Solution
Because my representative is still low enough to comment yet, I will only comment here and I will edit it if there is further clarification.
I don’t see any weights in the desired output. If I understand the json file correctly, then you are iterating over the price given the weight unit. is the expected output of looping through each item and traversing the price of each weight unit. If weight units are not available, enter NaN. Is there a possible list of weight units?
Pandas also has read_json functions, so you can load them directly into a Pandas dataframe.
— Edited —
Apologies for the delay. Take a look at the answers below
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]
The above will lead 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