Question: In Python create a hash of a hash using given CSV data. It will take a string array which is the output of your function

In Python create a hash of a hash using given CSV data.
It will take a string array which is the output of your function my_data_transform, it will return a json string of hash of hash following this format: {COLUMN: {Value1: nbr_of_occurence_of_value_1, Value2: nbr_of_occurence_of_value_2,...},...} Order of Column will be the order they are in the header of the CSV (Gender first then Email, etc) Order of the Value will be the order they appear in each line from top left. Use STRINGS as keys (=> Do not use any symbol or any fancy things. It doesn't translate well in json. (no :Age => "Age"))
So far I have this:
Expected Return Value {"Gender":{"Male":3,"Female":2},"Email":{"yahoo.com":2,"hotmail.com":3},"Age":{"21->40":2,"66->99":2,"41->65":1},"City":{"Seattle":2,"Detroit":1,"Las Vegas":1,"Chicago":1},"Device":{"Safari iPhone":1,"Chrome Android":2,"Chrome":2},"Order At":{"afternoon":4,"morning":1}}
Return Value "{\"Gender\": {\"Male\": 3,\"Female\": 2},\"Email\": {\"yahoo.com\": 2,\"hotmail.com\": 3},\"Age\": {\"21->40\": 2,\"66->99\": 2,\"41->65\": 1},\"City\": {\"Seattle\": 2,\"Detroit\": 1,\"Las Vegas\": 1,\"Chicago\": 1},\"Device\": {\"Safari iPhone\": 1,\"Chrome Android\": 2,\"Chrome\": 2},\"Order At\": {\"afternoon\": 4,\"morning\": 1}}"
--------- code snip --------------
import json
"""
:type input: {String[]}
:rtype: string
"""
def my_data_process(input):
headers = input[0].split(",")
data =[line.split(",") for line in input[1:]]
# Filter headers
required_headers =["Gender", "Email", "Age", "City", "Device", "Order At"]
required_indices =[i for i, header in enumerate(headers) if header in required_headers]
# Dictionary to count occurrences of each key value
counts ={header: {} for header in required_headers}
# Process each row
for row in data:
for index in required_indices:
header = headers[index]
value = row[index].strip()
if value in counts[header]:
counts[header][value]+=1
else:
counts[header][value]=1
counts
return json.dumps(counts)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!