Учитывая приведенный ниже пример данных (10 записей): test_df = pd.DataFrame ({"PN_id": ["745d626b", "745d626b", "fce503fb", "df3d727e", "df3d727e", & ...

0
cjcrm 13 Янв 2021 в 19:54

1 ответ

Лучший ответ
import json


def promote(d):
    if list(d.keys()) == ['and']:
        for i in d['and']:
            yield from promote(i)
    else:
        yield d

parsed = []
data = {"PN_id": ["745d626b", "745d626b", "fce503fb", "df3d727e", "df3d727e", "56c00531", "72ebb2b3", "5d1bc5d3", "72ebb2b3", "5c32fc8a", "5c32fc8a"],
 "PN_raw": ['{"audience":{"and":[{"segment":"67537044-27db-4a0b-b5b7-362c9c5b2ba7"},{"tag":"BR","group":"ua_locale_country"},{"tag":"90_P******_BR","group":"******_CRM"}]}}',
            '{"audience":{"and":[{"segment":"67537044-27db-4a0b-b5b7-362c9c5b2ba7"},{"tag":"BR","group":"ua_locale_country"},{"tag":"90_P******_BR","group":"******_CRM"}]}}',
            '{"audience":{"and":[{"and":[{"segment":"850c8d94-1236-45a1-93fc-08b0337b4059"}]},{"and":[{"tag":"All_S****_ES","group":"******_CRM"}]}]}}',
            '{"audience":{"and":[{"segment":"67537044-27db-4a0b-b5b7-362c9c5b2ba7"},{"tag":"BR","group":"ua_locale_country"},{"tag":"All_S*****_BR","group":"******_CRM"}]}}',
            '{"audience":{"and":[{"segment":"67537044-27db-4a0b-b5b7-362c9c5b2ba7"},{"tag":"BR","group":"ua_locale_country"},{"tag":"All_S*****_BR","group":"******_CRM"}]}}',
            '{"audience":{"and":[{"and":[{"segment":"850c8d94-1236-45a1-93fc-08b0337b4059"}]},{"and":[{"tag":"All_S****_ES","group":"******_CRM"}]}]}}',
            '{"audience":{"and":[{"segment":"67537044-27db-4a0b-b5b7-362c9c5b2ba7"},{"tag":"BR","group":"ua_locale_country"},{"tag":"P_90_or_S_90_BR","group":"******_CRM"}]}}',
            '{"audience":{"and":[{"segment":"67537044-27db-4a0b-b5b7-362c9c5b2ba7"},{"tag":"P_90_or_S_90_ESLA","group":"******_CRM"}]}}',
            '{"audience":{"and":[{"segment":"67537044-27db-4a0b-b5b7-362c9c5b2ba7"},{"tag":"BR","group":"ua_locale_country"},{"tag":"P_90_or_S_90_BR","group":"******_CRM"}]}}',
            '{"audience":{"and":[{"and":[{"segment":"850c8d94-1236-45a1-93fc-08b0337b4059"}]},{"and":[{"tag":"P_90_or_S_90_ES","group":"******_CRM"}]}]}}',
            '{"audience":{"and":[{"and":[{"segment":"850c8d94-1236-45a1-93fc-08b0337b4059"}]},{"and":[{"tag":"P_90_or_S_90_ES","group":"******_CRM"}]}]}}']}

data['PN'] = list(map(json.loads, data['PN_raw']))
for ind, pn_id in enumerate(data['PN_id']):
    parsed_sub = {'PN_id': pn_id}
    count = 1
    for chunk in promote(data['PN'][ind]['audience']):
        if 'segment' in chunk:
            parsed_sub.update(chunk)
        else:
            parsed_sub.update({f'{k}{count}': v for k, v in chunk.items()})
            count +=1
    parsed.append(parsed_sub)

df = pd.DataFrame(parsed)

Я обнаружил, что pandas запутывает / искажает JSON, и я предпочитаю обрабатывать его на базовом питоне. Я бы сказал, что JSON может иметь такие разные формы, что нет отличного способа создать универсальную функцию make_the_json_flat (), хотя я бы хотел узнать об этом, если такая вещь существует.

1
ccluff 13 Янв 2021 в 17:43