У меня есть следующий вложенный словарь, который содержит информацию, которую человек публично сообщили в организацию.
`person_json = {'basicInformation': {'individualId': 5429958,
'firstName': 'BARBARA','middleName': 'JEAN','lastName': 'ABADI',
'sanctions': {'permanentBar': 'Y',
'sanctionDetails': [{'category': 'BAR',
'regulator': 'FINRA',
'messages': ['FINRA_BAR_ALL_MESSAGE'],
'detail': [],
'capacity': ['ALL']}]},
'otherNames': [],
'bcScope': 'InActive',
'iaScope': 'NotInScope',
'daysInIndustry': 3185},
'currentEmployments': [],
'currentIAEmployments': [],
'previousEmployments': [{'iaOnly': 'N',
'bdSECNumber': '44376',
'firmId': 29705,
'firmName': 'ACGM, INC.',
'street1': '590 MADISON AVENUE, 41ST FLOOR, SUITE 4103',
'city': 'NEW YORK',
'state': 'NY',
'zipCode': '10022',
'registrationBeginDate': '6/1/2011',
'registrationEndDate': '2/1/2017',
'firmBCScope': 'INACTIVE',
'firmIAScope': 'NOTINSCOPE'},
{'iaOnly': 'N',
'bdSECNumber': '49900',
'firmId': 42619,
'firmName': 'ACTIVA CAPITAL MARKETS, INC.',
'street1': '590 MADISON AVENUE',
'street2': '41ST FLOOR',
'city': 'NEW YORK',
'state': 'NY',
'zipCode': '10022',
'registrationBeginDate': '2/1/2008',
'registrationEndDate': '2/16/2011',
'firmBCScope': 'INACTIVE',
'firmIAScope': 'NOTINSCOPE'}],
'previousIAEmployments': [],
'disclosureFlag': 'Y',
'iaDisclosureFlag': 'N',
'disclosures': [{'eventDate': '4/4/2017',
'disclosureType': 'Regulatory',
'disclosureResolution': 'Final',
'isIapdExcludedCCFlag': 'N',
'isBcExcludedCCFlag': 'N',
'bcCtgryType': 11,
'disclosureDetail': {'DocketNumberFDA': '2015044587502',
'DocketNumberAAO': '2015044587502',
'Initiated By': 'FINRA',
'Allegations': 'Without admitting or denying the findings, Abadi consented to the
sanctions and to the entry of findings that she refused to produce documents and information and to appear for on-the-record testimony requested by FINRA in connection with its investigation concerning certain suspicious fund transfers involving her member firm and affiliates thereof indirectly owned and controlled by Abadi.',
'Resolution': 'Acceptance, Waiver & Consent(AWC)',
'SanctionDetails': [{'Sanctions': 'Bar (Permanent)',
'SanctionDetails': [{'Registration Capacities Affected': 'All Capacities',
'Duration': 'Indefinite',
'Start Date': '4/4/2017'}]}]}}],
'examsCount': {'stateExamCount': 1,
'principalExamCount': 1,
'productExamCount': 2},
'stateExamCategory': [{'examCategory': 'Series 63',
'examName': 'Uniform Securities Agent State Law Examination',
'examTakenDate': '3/13/2008',
'examScope': 'BC'}],
'principalExamCategory': [{'examCategory': 'Series 24',
'examName': 'General Securities Principal Examination',
'examTakenDate': '9/26/2008',
'examScope': 'BC'}],
'productExamCategory': [{'examCategory': 'SIE',
'examName': 'Securities Industry Essentials Examination',
'examTakenDate': '2/1/2017',
'examScope': 'BC'},
{'examCategory': 'Series 7',
'examName': 'General Securities Representative Examination',
'examTakenDate': '1/31/2008',
'examScope': 'BC'}],
'registrationCount': {'approvedSRORegistrationCount': 0,
'approvedFinraRegistrationCount': 0,
'approvedStateRegistrationCount': 0},
'registeredStates': [],
'registeredSROs': [],
'brokerDetails': {'hasBCComments': 'N',
'hasIAComments': 'N',
'legacyReportStatusDescription': 'Not Requested'}}`
Что я пытаюсь сделать, так это свести информацию в приведенный выше вложенный словарь. Это код, который я использовал. Таблица называется «test1».
`test1=pds.json_normalize(person_json)`
То, что я получил от «test1», представляет собой следующую таблицу. Я сделал фото какой-то части стола.
Как вы можете видеть, в «previousEmployments», «disclosures», «stateExamCategory», «principalExamCategory», «productExamCategory» все еще есть список, и я пытаюсь сгладить эти данные и объединить их вместе в формате таблицы.
Ожидаемым результатом должен быть фрейм данных/таблица, содержащая всю информацию из всех ключей и всех «подключей» person_json.
Как я мог это сделать?
Спасибо,






Предполагая, что вы игнорируете то, как должна выглядеть ваша итоговая таблица/df, вот предложение с flatten_json / json_normalize для создания DataFrame, где столбцы являются внешними ключами person_json :
#pip install flatten_json
from flatten_json import flatten
tmp = pd.json_normalize(flatten(person_json))
df = (
tmp.set_axis(tmp.columns.str.split("_", n=1, expand=True), axis=1)
.stack(1).droplevel(0)
)
Выход :
print(df)
basicInformation ... stateExamCategory
NaN NaN ... NaN
0_bcCtgryType NaN ... NaN
0_bdSECNumber NaN ... NaN
... ... ... ...
sanctions_sanctionDetails_0_messages_0 FINRA_BAR_ALL_MESSAGE ... NaN
sanctions_sanctionDetails_0_regulator FINRA ... NaN
stateExamCount NaN ... NaN
[72 rows x 16 columns]
Если вы хотите получить определенную часть/фрагмент, вы можете использовать что-то вроде:
query = df.filter(regex = ".*ExamCount.*", axis=0).dropna(how = "all", axis=1)
# query = df.filter(regex = ".*ExamCount.*", axis=0).loc[:, "examsCount"] #variant
print(query)
examsCount
principalExamCount 1.00
productExamCount 2.00
stateExamCount 1.00
Большое спасибо. Это устраняет мою проблему, и последняя часть, которую мне, возможно, придется сделать, это создать фрейм данных.