Python Forum
Get specific key from multiple keys in python dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get specific key from multiple keys in python dictionary
#1
I have CSV like this(below is the sample)

Month Seg Zone Dist SALES
2018-12 HIGH A-ZONE NY 200
2018-12 LOW A-ZONE NY 100
2018-12 MEDIUM A-ZONE NY 300
And So on....
Out of above csv file I have created sql operation like in python

In SQL I would do like this
SELECT Month,Seg,SUM(Sales)
FROM CSV
group by Month,Seg

Same like I have created in python

with open('/Sales.csv','r') as sixm_sales:
ca_dict_reader = csv.DictReader(sixm_sales,delimiter='|')
agg_6m = defaultdict(float)
for row in ca_dict_reader:
   agg_6m[row["Month"],row["Seg"]] += float(row["Sales"])
print(agg_6m)
Dict agg_6m which gives below output like this

defaultdict(<class 'float'>, {('2018-12', 'HIGH'): 2945.17, ('2018-12', 'LOW'): 5064.99, ('2018-12', 'MEDIUM'): 5036.360000000001, ('2018-12', 'NON_TARGET'): 33.0, ('2018-11', 'HIGH'): 3719.67, ('2018-11', 'LOW'): 5029.84, ('2018-11', 'MEDIUM'): 4899.49, ('2018-11', 'NON_TARGET'): 36.0, ('2018-10', 'HIGH'): 4381.83, ('2018-10', 'LOW'): 4826.76, ('2018-10', 'MEDIUM'): 4762.869999999999, ('2018-10', 'NON_TARGET'): 32.0, ('2018-09', 'HIGH'): 2788.67, ('2018-09', 'LOW'): 4754.07, ('2018-09', 'MEDIUM'): 4803.16, ('2018-09', 'NON_TARGET'): 28.0, ('2018-08', 'HIGH'): 3915.5, ('2018-08', 'LOW'): 5220.99, ('2018-08', 'MEDIUM'): 5254.34, ('2018-08', 'NON_TARGET'): 36.0, ('2018-07', 'HIGH'): 4172.33, ('2018-07', 'LOW'): 4619.33, ('2018-07', 'MEDIUM'): 4516.65, ('2018-07', 'NON_TARGET'): 30.0})
From this I am trying to create JSON like below

"goal": {
"title": "Six Month Sales",
"value": 190,
"percentage": "-5.0",
"max": 9.84,
"min": 2.23,
"category": ["2018-07","2018-08","2018-09","2018-10","2018-11","2018-12"],
"xaxistitle": "Month",
"yaxistitle": "Sales",
"series": [
{
"Name": "MEDIUM",
"data": [3,4,6.11,4.19,8.96,6.08,2.23]
}
}

From agg_6m dictionary I have to create JSON output like above where Month will go in Category , Seg in name and sales in data. How this can be done?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Dict from multiple Lists with duplicate Keys rhat398 10 579 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  Adding keys and values to a dictionary giladal 3 597 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  access dictionary with keys from another and write values to list redminote4dd 6 1,126 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 1,193 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 779 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 1,451 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Delete specific lines contain specific words mannyi 2 1,883 Nov-04-2019, 04:50 PM
Last Post: mannyi
  Retrieving dictionary keys within with another dictionay bazcurtis 8 1,136 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  json.dumps to keep dictionary keys batchenr 1 832 May-14-2019, 11:17 AM
Last Post: buran
  Reference new dictionary keys with a variable slouw 4 1,130 May-07-2019, 03:30 AM
Last Post: slouw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020