У меня большой набор данных с 2000+ строками, я хочу преобразовать его в формат Specifi c Json. Я пробовал этот код на примере набора данных.
Я пытался использовать to_ json, to_dict, но он дает вывод в формате generi c.
import pandas as pd
from collections import defaultdict
data = [['food', 'vegatables', 10], ['food', 'fruits', 5], ['food', 'pulses', 12], ['cloth', 'shirts',2], ['cloth', 'trousers', 6], ['books', 'notebook', 3], ['pens', 'roller', 4], ['pens', 'ball', 3]]
df = pd.DataFrame(data, columns = ['Items', 'Subitem', 'Quantity'])
labels = defaultdict(int)
labels1 = defaultdict(int)
for cat in df["Items"]:
labels[cat] += 1
for sub in df["Subitem"]:
labels1[sub] += 1
check = [{"item": i, "weight": labels[i], 'groups':[{"subitem":j, "weight": labels1[j], "group" : [] } for j in labels1] } for i in labels]
check
Я получаю вывод как это
[{'item': 'food',
'weight': 3,
'groups': [{'subitem': 'vegatables', 'weight': 1, 'group': []},
{'subitem': 'fruits', 'weight': 1, 'group': []},
{'subitem': 'pulses', 'weight': 1, 'group': []},
{'subitem': 'shirts', 'weight': 1, 'group': []},
{'subitem': 'trousers', 'weight': 1, 'group': []},
{'subitem': 'notebook', 'weight': 1, 'group': []},
{'subitem': 'roller', 'weight': 1, 'group': []},
{'subitem': 'ball', 'weight': 1, 'group': []}]},
{'item': 'cloth',
'weight': 2,
'groups': [{'subitem': 'vegatables', 'weight': 1, 'group': []},
{'subitem': 'fruits', 'weight': 1, 'group': []},
{'subitem': 'pulses', 'weight': 1, 'group': []},
{'subitem': 'shirts', 'weight': 1, 'group': []},
{'subitem': 'trousers', 'weight': 1, 'group': []},
{'subitem': 'notebook', 'weight': 1, 'group': []},
{'subitem': 'roller', 'weight': 1, 'group': []},
{'subitem': 'ball', 'weight': 1, 'group': []}]},
{'item': 'books',
'weight': 1,
'groups': [{'subitem': 'vegatables', 'weight': 1, 'group': []},
{'subitem': 'fruits', 'weight': 1, 'group': []},
{'subitem': 'pulses', 'weight': 1, 'group': []},
{'subitem': 'shirts', 'weight': 1, 'group': []},
{'subitem': 'trousers', 'weight': 1, 'group': []},
{'subitem': 'notebook', 'weight': 1, 'group': []},
{'subitem': 'roller', 'weight': 1, 'group': []},
{'subitem': 'ball', 'weight': 1, 'group': []}]},
{'item': 'pens',
'weight': 2,
'groups': [{'subitem': 'vegatables', 'weight': 1, 'group': []},
{'subitem': 'fruits', 'weight': 1, 'group': []},
{'subitem': 'pulses', 'weight': 1, 'group': []},
{'subitem': 'shirts', 'weight': 1, 'group': []},
{'subitem': 'trousers', 'weight': 1, 'group': []},
{'subitem': 'notebook', 'weight': 1, 'group': []},
{'subitem': 'roller', 'weight': 1, 'group': []},
{'subitem': 'ball', 'weight': 1, 'group': []}]}]
Но я хочу вывод, который имеет только подэлементы, которые относятся к этому элементу
[{'item': 'food',
'weight': 3,
'groups': [
{'subitem': 'vegatables', 'weight': 10, 'group': []},
{'subitem': 'fruits', 'weight': 5, 'group': []},
{'subitem': 'pulses', 'weight': 12, 'group': []}]},
{'item': 'cloth',
'weight': 2,
'groups': [
{'subitem': 'shirts', 'weight': 2, 'group': []},
{'subitem': 'trousers', 'weight': 6, 'group': []}]},
{'item': 'books',
'weight': 1,
'groups': [
{'subitem': 'notebook', 'weight': 3, 'group': []}]},
{'item': 'pens',
'weight': 2,
'groups': [
{'subitem': 'roller', 'weight': 4, 'group': []},
{'subitem': 'ball', 'weight': 3, 'group': []}]}]
И что нужно сделать, если нужен вывод, подобный этому (где вес элемента суммируется с весами подпункта).
[{'item': 'food',
'weight': 27,
'groups': [
{'subitem': 'vegatables', 'weight': 10, 'group': []},
{'subitem': 'fruits', 'weight': 5, 'group': []},
{'subitem': 'pulses', 'weight': 12, 'group': []}]},
{'item': 'cloth',
'weight': 8,
'groups': [
{'subitem': 'shirts', 'weight': 2, 'group': []},
{'subitem': 'trousers', 'weight': 6, 'group': []}]},
{'item': 'books',
'weight': 3,
'groups': [
{'subitem': 'notebook', 'weight': 3, 'group': []}]},
{'item': 'pens',
'weight': 7,
'groups': [
{'subitem': 'roller', 'weight': 4, 'group': []},
{'subitem': 'ball', 'weight': 3, 'group': []}]}]