Wil*_*iam 10 python json python-itertools dataframe pandas
背景
我有一个复杂的嵌套 JSON 对象,我试图df以一种非常具体的方式将其解压到 pandas 中。
JSON 对象
这是一个摘录,包含 JSON 对象的随机数据,其中显示了 1x 家族(即“Falconer Family”)的层次结构示例(包括子项),但是总共有 100 个,此摘录只有 1x 系列,但是完整的 JSON 对象有多个 -
{\n "meta": {\n "columns": [{\n "key": "value",\n "display_name": "Adjusted Value (No Div, USD)",\n "output_type": "Number",\n "currency": "USD"\n },\n {\n "key": "time_weighted_return",\n "display_name": "Current Quarter TWR (USD)",\n "output_type": "Percent",\n "currency": "USD"\n },\n {\n "key": "time_weighted_return_2",\n "display_name": "YTD TWR (USD)",\n "output_type": "Percent",\n "currency": "USD"\n },\n {\n "key": "_custom_twr_audit_note_911328",\n "display_name": "TWR Audit Note",\n "output_type": "Word"\n }\n ],\n "groupings": [{\n "key": "_custom_name_747205",\n "display_name": "* Reporting Client Name"\n },\n {\n "key": "_custom_new_entity_group_453577",\n "display_name": "NEW Entity Group"\n },\n {\n "key": "_custom_level_2_624287",\n "display_name": "* Level 2"\n },\n {\n "key": "legal_entity",\n "display_name": "Legal Entity"\n }\n ]\n },\n "data": {\n "type": "portfolio_views",\n "attributes": {\n "total": {\n "name": "Total",\n "columns": {\n "time_weighted_return": -0.046732301295604683,\n "time_weighted_return_2": -0.046732301295604683,\n "_custom_twr_audit_note_911328": null,\n "value": 23132492.905107163\n },\n "children": [{\n "name": "Falconer Family",\n "grouping": "_custom_name_747205",\n "columns": {\n "time_weighted_return": -0.046732301295604683,\n "time_weighted_return_2": -0.046732301295604683,\n "_custom_twr_audit_note_911328": null,\n "value": 23132492.905107163\n },\n "children": [{\n "name": "Wealth Bucket A",\n "grouping": "_custom_new_entity_group_453577",\n "columns": {\n "time_weighted_return": -0.045960317420568164,\n "time_weighted_return_2": -0.045960317420568164,\n "_custom_twr_audit_note_911328": null,\n "value": 13264448.506587159\n },\n "children": [{\n "name": "Asset Class A",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": 0.000003434094574039648,\n "time_weighted_return_2": 0.000003434094574039648,\n "_custom_twr_audit_note_911328": null,\n "value": 3337.99\n },\n "children": [{\n "entity_id": 10604454,\n "name": "HUDJ Trust",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": 0.000003434094574039648,\n "time_weighted_return_2": 0.000003434094574039648,\n "_custom_twr_audit_note_911328": null,\n "value": 3337.99\n },\n "children": []\n }]\n },\n {\n "name": "Asset Class B",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": -0.025871339096964152,\n "time_weighted_return_2": -0.025871339096964152,\n "_custom_twr_audit_note_911328": null,\n "value": 1017004.7192636987\n },\n "children": [{\n "entity_id": 10604454,\n "name": "HUDG Trust",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.025871339096964152,\n "time_weighted_return_2": -0.025871339096964152,\n "_custom_twr_audit_note_911328": null,\n "value": 1017004.7192636987\n },\n "children": []\n }]\n },\n {\n "name": "Asset Class C",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": -0.030370376329670656,\n "time_weighted_return_2": -0.030370376329670656,\n "_custom_twr_audit_note_911328": null,\n "value": 231142.67772000004\n },\n "children": [{\n "entity_id": 10604454,\n "name": "HKDJ Trust",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.030370376329670656,\n "time_weighted_return_2": -0.030370376329670656,\n "_custom_twr_audit_note_911328": null,\n "value": 231142.67772000004\n },\n "children": []\n }]\n },\n {\n "name": "Asset Class D",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": -0.05382756475465478,\n "time_weighted_return_2": -0.05382756475465478,\n "_custom_twr_audit_note_911328": null,\n "value": 9791282.570000006\n },\n "children": [{\n "entity_id": 10604454,\n "name": "HUDW Trust",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.05382756475465478,\n "time_weighted_return_2": -0.05382756475465478,\n "_custom_twr_audit_note_911328": null,\n "value": 9791282.570000006\n },\n "children": []\n }]\n },\n {\n "name": "Asset Class E",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": -0.01351630404081805,\n "time_weighted_return_2": -0.01351630404081805,\n "_custom_twr_audit_note_911328": null,\n "value": 2153366.6396034593\n },\n "children": [{\n "entity_id": 10604454,\n "name": "HJDJ Trust",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.01351630404081805,\n "time_weighted_return_2": -0.01351630404081805,\n "_custom_twr_audit_note_911328": null,\n "value": 2153366.6396034593\n },\n "children": []\n }]\n },\n {\n "name": "Asset Class F",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": -0.002298190175237247,\n "time_weighted_return_2": -0.002298190175237247,\n "_custom_twr_audit_note_911328": null,\n "value": 68313.90999999999\n },\n "children": [{\n "entity_id": 10604454,\n "name": "HADJ Trust",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.002298190175237247,\n "time_weighted_return_2": -0.002298190175237247,\n "_custom_twr_audit_note_911328": null,\n "value": 68313.90999999999\n },\n "children": []\n }]\n }\n ]\n },\n {\n "name": "Wealth Bucket B",\n "grouping": "_custom_new_entity_group_453577",\n "columns": {\n "time_weighted_return": -0.04769870075659244,\n "time_weighted_return_2": -0.04769870075659244,\n "_custom_twr_audit_note_911328": null,\n "value": 9868044.398519998\n },\n "children": [{\n "name": "Asset Class A",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": 0.000028632718065191298,\n "time_weighted_return_2": 0.000028632718065191298,\n "_custom_twr_audit_note_911328": null,\n "value": 10234.94\n },\n "children": [{\n "entity_id": 10868778,\n "name": "2012 Desc Tr HBO Thalia",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": 0.0000282679297198829,\n "time_weighted_return_2": 0.0000282679297198829,\n "_custom_twr_audit_note_911328": null,\n "value": 244.28\n },\n "children": []\n },\n {\n "entity_id": 10643052,\n "name": "2013 Irrev Tr HBO Thalia",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": 0.000049373572795108345,\n "time_weighted_return_2": 0.000049373572795108345,\n "_custom_twr_audit_note_911328": null,\n "value": 5081.08\n },\n "children": []\n },\n {\n "entity_id": 10598341,\n "name": "Cht 11th Tr HBO Shirley",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": 0.000006609603754315074,\n "time_weighted_return_2": 0.000006609603754315074,\n "_custom_twr_audit_note_911328": null,\n "value": 1523.62\n },\n "children": []\n },\n {\n "entity_id": 10598337,\n "name": "Cht 11th Tr HBO Hannah",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": 0.000010999769004760296,\n "time_weighted_return_2": 0.000010999769004760296,\n "_custom_twr_audit_note_911328": null,\n "value": 1828.9\n },\n "children": []\n },\n {\n "entity_id": 10598334,\n "name": "Cht 11th Tr HBO Lau",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": 0.000006466673995619843,\n "time_weighted_return_2": 0.000006466673995619843,\n "_custom_twr_audit_note_911328": null,\n "value": 1557.06\n },\n "children": []\n }\n ]\n },\n {\n "name": "Asset Class B",\n "grouping": "_custom_level_2_624287",\n "columns": {\n "time_weighted_return": -0.024645947842438676,\n "time_weighted_return_2": -0.024645947842438676,\n "_custom_twr_audit_note_911328": null,\n "value": 674052.31962\n },\n "children": [{\n "entity_id": 10868778,\n "name": "2012 Desc Tr HBO Thalia",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.043304004172576405,\n "time_weighted_return_2": -0.043304004172576405,\n "_custom_twr_audit_note_911328": null,\n "value": 52800.96\n },\n "children": []\n },\n {\n "entity_id": 10643052,\n "name": "2013 Irrev Tr HBO Thalia",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.022408434778798836,\n "time_weighted_return_2": -0.022408434778798836,\n "_custom_twr_audit_note_911328": null,\n "value": 599594.11962\n },\n "children": []\n },\n {\n "entity_id": 10598341,\n "name": "Cht 11th Tr HBO Shirley",\n "grouping": "legal_entity",\n "columns": {\n "time_weighted_return": -0.039799855483646174,\n "time_weighted_return_2": -0.039799855483646174,\n "_custom_twr_audit_note_911328": null,\n "value": 7219.08\n },\n "children": []\n },\n
jsonpath-ng甚至可以非常轻松地解析这样的嵌套 json 对象。您可以通过以下命令安装这个方便的库:
pip install --upgrade jsonpath-ng\nRun Code Online (Sandbox Code Playgroud)\nimport json\nimport jsonpath_ng as jp\nimport pandas as pd\n\ndef unpack_response(r):\n # Create a dataframe from extracted data\n expr = jp.parse(\'$..children.[*]\')\n data = [{\'full_path\': str(m.full_path), **m.value} for m in expr.find(r)]\n df = pd.json_normalize(data).sort_values(\'full_path\', ignore_index=True)\n\n # Append a portfolio column\n df[\'portfolio\'] = df.loc[df.full_path.str.contains(r\'total\\.children\\.\\[\\d+]$\'), \'name\']\n df[\'portfolio\'].fillna(method=\'ffill\', inplace=True)\n\n # Deal with columns\n trans = {\'columns.\' + c[\'key\']: c[\'display_name\'] for c in r[\'meta\'][\'columns\']}\n cols = [\'full_path\', \'portfolio\', \'name\', \'entity_id\', \'Adjusted Value (No Div, USD)\', \'Current Quarter TWR (USD)\', \'YTD TWR (USD)\', \'TWR Audit Note\']\n df = df.rename(columns=trans)[cols]\n\n return df\n\n# Load the sample data from file\n# with open(\'api_response_2022-02-13.json\', \'r\') as f:\n# api_response = json.load(f)\n\n# Load the sample data from string\napi_response = json.loads(\'{"meta": {"columns": [{"key": "value", "display_name": "Adjusted Value (No Div, USD)", "output_type": "Number", "currency": "USD"}, {"key": "time_weighted_return", "display_name": "Current Quarter TWR (USD)", "output_type": "Percent", "currency": "USD"}, {"key": "time_weighted_return_2", "display_name": "YTD TWR (USD)", "output_type": "Percent", "currency": "USD"}, {"key": "_custom_twr_audit_note_911328", "display_name": "TWR Audit Note", "output_type": "Word"}], "groupings": [{"key": "_custom_name_747205", "display_name": "* Reporting Client Name"}, {"key": "_custom_new_entity_group_453577", "display_name": "NEW Entity Group"}, {"key": "_custom_level_2_624287", "display_name": "* Level 2"}, {"key": "legal_entity", "display_name": "Legal Entity"}]}, "data": {"type": "portfolio_views", "attributes": {"total": {"name": "Total", "columns": {"time_weighted_return": -0.046732301295604683, "time_weighted_return_2": -0.046732301295604683, "_custom_twr_audit_note_911328": null, "value": 23132492.905107163}, "children": [{"name": "Falconer Family", "grouping": "_custom_name_747205", "columns": {"time_weighted_return": -0.046732301295604683, "time_weighted_return_2": -0.046732301295604683, "_custom_twr_audit_note_911328": null, "value": 23132492.905107163}, "children": [{"name": "Wealth Bucket A", "grouping": "_custom_new_entity_group_453577", "columns": {"time_weighted_return": -0.045960317420568164, "time_weighted_return_2": -0.045960317420568164, "_custom_twr_audit_note_911328": null, "value": 13264448.506587159}, "children": [{"name": "Asset Class A", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": 3.434094574039648e-06, "time_weighted_return_2": 3.434094574039648e-06, "_custom_twr_audit_note_911328": null, "value": 3337.99}, "children": [{"entity_id": 10604454, "name": "HUDJ Trust", "grouping": "legal_entity", "columns": {"time_weighted_return": 3.434094574039648e-06, "time_weighted_return_2": 3.434094574039648e-06, "_custom_twr_audit_note_911328": null, "value": 3337.99}, "children": []}]}, {"name": "Asset Class B", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.025871339096964152, "time_weighted_return_2": -0.025871339096964152, "_custom_twr_audit_note_911328": null, "value": 1017004.7192636987}, "children": [{"entity_id": 10604454, "name": "HUDG Trust", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.025871339096964152, "time_weighted_return_2": -0.025871339096964152, "_custom_twr_audit_note_911328": null, "value": 1017004.7192636987}, "children": []}]}, {"name": "Asset Class C", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.030370376329670656, "time_weighted_return_2": -0.030370376329670656, "_custom_twr_audit_note_911328": null, "value": 231142.67772000004}, "children": [{"entity_id": 10604454, "name": "HKDJ Trust", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.030370376329670656, "time_weighted_return_2": -0.030370376329670656, "_custom_twr_audit_note_911328": null, "value": 231142.67772000004}, "children": []}]}, {"name": "Asset Class D", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.05382756475465478, "time_weighted_return_2": -0.05382756475465478, "_custom_twr_audit_note_911328": null, "value": 9791282.570000006}, "children": [{"entity_id": 10604454, "name": "HUDW Trust", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.05382756475465478, "time_weighted_return_2": -0.05382756475465478, "_custom_twr_audit_note_911328": null, "value": 9791282.570000006}, "children": []}]}, {"name": "Asset Class E", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.01351630404081805, "time_weighted_return_2": -0.01351630404081805, "_custom_twr_audit_note_911328": null, "value": 2153366.6396034593}, "children": [{"entity_id": 10604454, "name": "HJDJ Trust", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.01351630404081805, "time_weighted_return_2": -0.01351630404081805, "_custom_twr_audit_note_911328": null, "value": 2153366.6396034593}, "children": []}]}, {"name": "Asset Class F", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.002298190175237247, "time_weighted_return_2": -0.002298190175237247, "_custom_twr_audit_note_911328": null, "value": 68313.90999999999}, "children": [{"entity_id": 10604454, "name": "HADJ Trust", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.002298190175237247, "time_weighted_return_2": -0.002298190175237247, "_custom_twr_audit_note_911328": null, "value": 68313.90999999999}, "children": []}]}]}, {"name": "Wealth Bucket B", "grouping": "_custom_new_entity_group_453577", "columns": {"time_weighted_return": -0.04769870075659244, "time_weighted_return_2": -0.04769870075659244, "_custom_twr_audit_note_911328": null, "value": 9868044.398519998}, "children": [{"name": "Asset Class A", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": 2.8632718065191298e-05, "time_weighted_return_2": 2.8632718065191298e-05, "_custom_twr_audit_note_911328": null, "value": 10234.94}, "children": [{"entity_id": 10868778, "name": "2012 Desc Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": 2.82679297198829e-05, "time_weighted_return_2": 2.82679297198829e-05, "_custom_twr_audit_note_911328": null, "value": 244.28}, "children": []}, {"entity_id": 10643052, "name": "2013 Irrev Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": 4.9373572795108345e-05, "time_weighted_return_2": 4.9373572795108345e-05, "_custom_twr_audit_note_911328": null, "value": 5081.08}, "children": []}, {"entity_id": 10598341, "name": "Cht 11th Tr HBO Shirley", "grouping": "legal_entity", "columns": {"time_weighted_return": 6.609603754315074e-06, "time_weighted_return_2": 6.609603754315074e-06, "_custom_twr_audit_note_911328": null, "value": 1523.62}, "children": []}, {"entity_id": 10598337, "name": "Cht 11th Tr HBO Hannah", "grouping": "legal_entity", "columns": {"time_weighted_return": 1.0999769004760296e-05, "time_weighted_return_2": 1.0999769004760296e-05, "_custom_twr_audit_note_911328": null, "value": 1828.9}, "children": []}, {"entity_id": 10598334, "name": "Cht 11th Tr HBO Lau", "grouping": "legal_entity", "columns": {"time_weighted_return": 6.466673995619843e-06, "time_weighted_return_2": 6.466673995619843e-06, "_custom_twr_audit_note_911328": null, "value": 1557.06}, "children": []}]}, {"name": "Asset Class B", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.024645947842438676, "time_weighted_return_2": -0.024645947842438676, "_custom_twr_audit_note_911328": null, "value": 674052.31962}, "children": [{"entity_id": 10868778, "name": "2012 Desc Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.043304004172576405, "time_weighted_return_2": -0.043304004172576405, "_custom_twr_audit_note_911328": null, "value": 52800.96}, "children": []}, {"entity_id": 10643052, "name": "2013 Irrev Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.022408434778798836, "time_weighted_return_2": -0.022408434778798836, "_custom_twr_audit_note_911328": null, "value": 599594.11962}, "children": []}, {"entity_id": 10598341, "name": "Cht 11th Tr HBO Shirley", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.039799855483646174, "time_weighted_return_2": -0.039799855483646174, "_custom_twr_audit_note_911328": null, "value": 7219.08}, "children": []}, {"entity_id": 10598337, "name": "Cht 11th Tr HBO Hannah", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.039799855483646174, "time_weighted_return_2": -0.039799855483646174, "_custom_twr_audit_note_911328": null, "value": 7219.08}, "children": []}, {"entity_id": 10598334, "name": "Cht 11th Tr HBO Lau", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.039799855483646174, "time_weighted_return_2": -0.039799855483646174, "_custom_twr_audit_note_911328": null, "value": 7219.08}, "children": []}]}, {"name": "Asset Class C", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.03037038746301135, "time_weighted_return_2": -0.03037038746301135, "_custom_twr_audit_note_911328": null, "value": 114472.69744}, "children": [{"entity_id": 10868778, "name": "2012 Desc Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.030370390035505124, "time_weighted_return_2": -0.030370390035505124, "_custom_twr_audit_note_911328": null, "value": 114472.68744000001}, "children": []}, {"entity_id": 10643052, "name": "2013 Irrev Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": 0, "time_weighted_return_2": 0, "_custom_twr_audit_note_911328": null, "value": 0.01}, "children": []}]}, {"name": "Asset Class D", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.06604362523792162, "time_weighted_return_2": -0.06604362523792162, "_custom_twr_audit_note_911328": null, "value": 5722529.229999997}, "children": [{"entity_id": 10868778, "name": "2012 Desc Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.06154960593668424, "time_weighted_return_2": -0.06154960593668424, "_custom_twr_audit_note_911328": null, "value": 1191838.9399999995}, "children": []}, {"entity_id": 10643052, "name": "2013 Irrev Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.06750460387418267, "time_weighted_return_2": -0.06750460387418267, "_custom_twr_audit_note_911328": null, "value": 4416618.520000002}, "children": []}, {"entity_id": 10598341, "name": "Cht 11th Tr HBO Shirley", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.05604507809250081, "time_weighted_return_2": -0.05604507809250081, "_custom_twr_audit_note_911328": null, "value": 38190.33}, "children": []}, {"entity_id": 10598337, "name": "Cht 11th Tr HBO Hannah", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.05604507809250081, "time_weighted_return_2": -0.05604507809250081, "_custom_twr_audit_note_911328": null, "value": 37940.72}, "children": []}, {"entity_id": 10598334, "name": "Cht 11th Tr HBO Lau", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.05604507809250081, "time_weighted_return_2": -0.05604507809250081, "_custom_twr_audit_note_911328": null, "value": 37940.72}, "children": []}]}, {"name": "Asset Class E", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.017118805423322003, "time_weighted_return_2": -0.017118805423322003, "_custom_twr_audit_note_911328": null, "value": 3148495.0914600003}, "children": [{"entity_id": 10868778, "name": "2012 Desc Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.015251157805867277, "time_weighted_return_2": -0.015251157805867277, "_custom_twr_audit_note_911328": null, "value": 800493.06146}, "children": []}, {"entity_id": 10643052, "name": "2013 Irrev Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.01739609576880241, "time_weighted_return_2": -0.01739609576880241, "_custom_twr_audit_note_911328": null, "value": 2215511.2700000005}, "children": []}, {"entity_id": 10598341, "name": "Cht 11th Tr HBO Shirley", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.02085132265594647, "time_weighted_return_2": -0.02085132265594647, "_custom_twr_audit_note_911328": null, "value": 44031.21}, "children": []}, {"entity_id": 10598337, "name": "Cht 11th Tr HBO Hannah", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.02089393244695803, "time_weighted_return_2": -0.02089393244695803, "_custom_twr_audit_note_911328": null, "value": 44394.159999999996}, "children": []}, {"entity_id": 10598334, "name": "Cht 11th Tr HBO Lau", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.020607507059866248, "time_weighted_return_2": -0.020607507059866248, "_custom_twr_audit_note_911328": null, "value": 44065.39000000001}, "children": []}]}, {"name": "Asset Class F", "grouping": "_custom_level_2_624287", "columns": {"time_weighted_return": -0.0014710489231547497, "time_weighted_return_2": -0.0014710489231547497, "_custom_twr_audit_note_911328": null, "value": 198260.12}, "children": [{"entity_id": 10868778, "name": "2012 Desc Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.0014477244560456848, "time_weighted_return_2": -0.0014477244560456848, "_custom_twr_audit_note_911328": null, "value": 44612.33}, "children": []}, {"entity_id": 10643052, "name": "2013 Irrev Tr HBO Thalia", "grouping": "legal_entity", "columns": {"time_weighted_return": -0.001477821083437858, "time_weighted_return_2": -0.001477821083437858, "_custom_twr_audit_note_911328": null, "value": 153647.78999999998}, "children": []}]}]}]}]}}, "included": []}}\')\n\ndf = unpack_response(api_response)\nRun Code Online (Sandbox Code Playgroud)\n首先,您可以通过以下命令确认预期输出:
\nprint(df.iloc[:5:,1:])\nRun Code Online (Sandbox Code Playgroud)\n| 文件夹 | 姓名 | 实体ID | 调整值(无 Div,美元) | 本季度 TWR(美元) | 年初至今 TWR(美元) | TWR审计记录 |
|---|---|---|---|---|---|---|
| 福尔科纳家族 | 福尔科纳家族 | 南 | 2.31325e+07 | -0.0467323 | -0.0467323 | |
| 福尔科纳家族 | 财富桶A | 南 | 1.32644e+07 | -0.0459603 | -0.0459603 | |
| 福尔科纳家族 | A类资产 | 南 | 3337.99 | 3.43409e-06 | 3.43409e-06 | |
| 福尔科纳家族 | HUDJ 信托基金 | 1.06045e+07 | 3337.99 | 3.43409e-06 | 3.43409e-06 | |
| 福尔科纳家族 | B类资产 | 南 | 1.017e+06 | -0.0258713 | -0.0258713 |
jsonpath-ng随后,您可以通过以下命令看到其中的精彩功能之一:
print(df.iloc[:10,:3])\nRun Code Online (Sandbox Code Playgroud)\n| 完整路径 | 文件夹 | 姓名 |
|---|---|---|
| 数据.attributes.total.children.[0] | 福尔科纳家族 | 福尔科纳家族 |
| data.attributes.total.children.[0].children.[0] | 福尔科纳家族 | 财富桶A |
| data.attributes.total.children.[0].children.[0].children.[0] | 福尔科纳家族 | A类资产 |
| data.attributes.total.children.[0].children.[0].children.[0].children.[0] | 福尔科纳家族 | HUDJ 信托基金 |
| data.attributes.total.children.[0].children.[0].children.[1] | 福尔科纳家族 | B类资产 |
| data.attributes.total.children.[0].children.[0].children.[1].children.[0] | 福尔科纳家族 | HUDG信托 |
| data.attributes.total.children.[0].children.[0].children.[2] | 福尔科纳家族 | C类资产 |
| data.attributes.total.children.[0].children.[0].children.[2].children.[0] | 福尔科纳家族 | 香港DJ信托 |
| data.attributes.total.children.[0].children.[0].children.[3] | 福尔科纳家族 | 资产类别 D |
| data.attributes.total.children.[0].children.[0].children.[3].children.[0] | 福尔科纳家族 | HUDW 信托 |
借助该full_path列,您可以即时掌握每行中提取的数据的嵌套级别。实际上,我portfolio通过使用这些路径附加了正确的值。
就代码而言,关键点在于以下行:
\nexpr = jp.parse(\'$..children.[*]\')\nRun Code Online (Sandbox Code Playgroud)\n通过上面的表达式,可以查询childrenjson对象任意级别的属性。README.rst告诉您每种语法代表什么。
| 句法 | 意义 |
|---|---|
$ | 根对象 |
jsonpath1 .. jsonpath2 | 与 jsonpath2 匹配且源自与 jsonpath1 匹配的任何节点的所有节点 |
[*] | 任意数组索引 |