'How to convert rows under a column into a list under a dictionary under a list under a dictionary?
I have an excel file with the below column name as variantID and corresponding elements. I want the final output as {"filters":[{"tags":[{"k":201,"v":"201"},{"k":202,"v":"202"}],"name":"custom-variants","show":true,"isSelected":false}],"start":0}, in which values for k and v will keep on increasing inside the list, which is in place of "value" in key-value pair of "tags", as per the number of rows and corresponding elemnts in the variantID column, however everything such as "filters", "tags", "name":"custom-variants","show":true,"isSelected":false must be constant. I have used the below code to get k, v values as per the data provided below :-
import pandas as pd
import json
data = pd.read_excel("C:\\Users\\Bijnis\\Desktop\\Variant_ID.xlsx")
data['variantID1'] = data['variantID'] / 1
data['variantID1'] = data['variantID'].astype(int)
data['variantID1'] = data['variantID'].astype(str)
data.rename(columns = {'variantID' : 'k', 'variantID1' : 'v'}, inplace = True)
f=data.to_dict('records')
z = print(json.dumps(f))
My output so far has been till this only : [{"k": 201, "v": "201"}, {"k": 202, "v": "202"}, {"k": 203, "v": "203"}, {"k": 204, "v": "204"}, {"k": 205, "v": "205"}]
But I have no clue about how to keep other things constant and get the output as mentioned above.
variantID 201 202 203 204 205
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|