Explore all Pandas open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Pandas

scikit-learn

scikit-learn 1.0.2

pandas

Pandas 1.4.1

streamlit

1.8.1

seaborn

v0.11.2 (August 2021)

notebook

v7.0.0a2

Popular Libraries in Pandas

scikit-learn

by scikit-learn doticonpythondoticon

star image 49728 doticonBSD-3-Clause

scikit-learn: machine learning in Python

pandas

by pandas-dev doticonpythondoticon

star image 33259 doticonBSD-3-Clause

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more

PythonDataScienceHandbook

by jakevdp doticonjupyter notebookdoticon

star image 32215 doticonNOASSERTION

Python Data Science Handbook: full text in Jupyter Notebooks

streamlit

by streamlit doticonpythondoticon

star image 18692 doticonApache-2.0

Streamlit — The fastest way to build data apps in Python

Complete-Python-3-Bootcamp

by Pierian-Data doticonjupyter notebookdoticon

star image 17053 doticon

Course Files for Complete Python 3 Bootcamp Course on Udemy

handson-ml2

by ageron doticonjupyter notebookdoticon

star image 16775 doticonApache-2.0

A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.

pydata-book

by wesm doticonjupyter notebookdoticon

star image 15489 doticonNOASSERTION

Materials and IPython notebooks for "Python for Data Analysis" by Wes McKinney, published by O'Reilly Media

tushare

by waditu doticonpythondoticon

star image 11075 doticonBSD-3-Clause

TuShare is a utility for crawling historical data of China stocks

dask

by dask doticonpythondoticon

star image 9771 doticonBSD-3-Clause

Parallel computing with task scheduling

Trending New libraries in Pandas

polars

by pola-rs doticonrustdoticon

star image 5341 doticonMIT

Fast multi-threaded DataFrame library in Rust | Python | Node.js

lux

by lux-org doticonpythondoticon

star image 3417 doticonApache-2.0

Automatically visualize your pandas dataframe via a single print! 📊 💡

evidently

by evidentlyai doticonpythondoticon

star image 2268 doticonApache-2.0

Evaluate and monitor ML models from validation to production. Join our Discord: https://discord.com/invite/xZjKRaNp8b

sweetviz

by fbdesignpro doticonpythondoticon

star image 1597 doticonMIT

Visualize and compare datasets, target values and associations, with one line of code.

tda-api

by alexgolec doticonpythondoticon

star image 958 doticonMIT

A TD Ameritrade API client for Python. Includes historical data for equities and ETFs, options chains, streaming order book data, complex order construction, and more.

pygooglenews

by kotartemiy doticonpythondoticon

star image 933 doticonMIT

If Google News had a Python library

Fast-F1

by theOehrly doticonpythondoticon

star image 764 doticonMIT

FastF1 is a python package for accessing and analyzing Formula 1 results, schedules, timing data and telemetry

DataProfiler

by capitalone doticonpythondoticon

star image 747 doticonApache-2.0

What's in your data? Extract schema, statistics and entities from datasets

ppscore

by 8080labs doticonpythondoticon

star image 663 doticonMIT

Predictive Power Score (PPS) in Python

Top Authors in Pandas

1

PacktPublishing

36 Libraries

star icon447

2

datadesk

17 Libraries

star icon823

3

pandastream

7 Libraries

star icon110

4

fonnesbeck

6 Libraries

star icon1389

5

spacetelescope

6 Libraries

star icon74

6

apachecn

6 Libraries

star icon2438

7

omnisci

6 Libraries

star icon66

8

compute-io

6 Libraries

star icon27

9

Data-Wrangling-with-JavaScript

6 Libraries

star icon24

10

jseabold

5 Libraries

star icon466

1

36 Libraries

star icon447

2

17 Libraries

star icon823

3

7 Libraries

star icon110

4

6 Libraries

star icon1389

5

6 Libraries

star icon74

6

6 Libraries

star icon2438

7

6 Libraries

star icon66

8

6 Libraries

star icon27

10

5 Libraries

star icon466

Trending Kits in Pandas

No Trending Kits are available at this moment for Pandas

Trending Discussions on Pandas

Installing scipy and scikit-learn on apple m1

Error while downloading the requirements using pip install (setup command: use_2to3 is invalid.)

Mapping complex JSON to Pandas Dataframe

AttributeError: Can't get attribute 'new_block' on <module 'pandas.core.internals.blocks'>

How to update pandas DataFrame.drop() for Future Warning - all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only

Cannot set up a conda environment with python 3.10

ImportError: cannot import name 'ABCIndexClass' from 'pandas.core.dtypes.generic'

Merge two pandas DataFrame based on partial match

Create a new column in a Pandas DataFrame from existing column names

After conda update, python kernel crashes when matplotlib is used

QUESTION

Installing scipy and scikit-learn on apple m1

Asked 2022-Mar-22 at 06:21

The installation on the m1 chip for the following packages: Numpy 1.21.1, pandas 1.3.0, torch 1.9.0 and a few other ones works fine for me. They also seem to work properly while testing them. However when I try to install scipy or scikit-learn via pip this error appears:

ERROR: Failed building wheel for numpy

Failed to build numpy

ERROR: Could not build wheels for numpy which use PEP 517 and cannot be installed directly

Why should Numpy be build again when I have the latest version from pip already installed?

Every previous installation was done using python3.9 -m pip install ... on Mac OS 11.3.1 with the apple m1 chip.

Maybe somebody knows how to deal with this error or if its just a matter of time.

ANSWER

Answered 2021-Aug-02 at 14:33

Please see this note of scikit-learn about

Installing on Apple Silicon M1 hardware

The recently introduced macos/arm64 platform (sometimes also known as macos/aarch64) requires the open source community to upgrade the build configuation and automation to properly support it.

At the time of writing (January 2021), the only way to get a working installation of scikit-learn on this hardware is to install scikit-learn and its dependencies from the conda-forge distribution, for instance using the miniforge installers:

https://github.com/conda-forge/miniforge

The following issue tracks progress on making it possible to install scikit-learn from PyPI with pip:

https://github.com/scikit-learn/scikit-learn/issues/19137

Source https://stackoverflow.com/questions/68620927

QUESTION

Error while downloading the requirements using pip install (setup command: use_2to3 is invalid.)

Asked 2022-Mar-05 at 07:13

version pip 21.2.4 python 3.6

The command:

1pip install -r  requirments.txt
2

The content of my requirements.txt:

1pip install -r  requirments.txt
2mongoengine==0.19.1
3numpy==1.16.2
4pylint
5pandas==1.1.5
6fawkes
7

The command is failing with this error

1pip install -r  requirments.txt
2mongoengine==0.19.1
3numpy==1.16.2
4pylint
5pandas==1.1.5
6fawkes
7ERROR: Command errored out with exit status 1:
8     command: /Users/*/Desktop/ml/*/venv/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/kn/0y92g7x55qs7c42tln4gwhtm0000gp/T/pip-install-soh30mel/mongoengine_89e68f8427244f1bb3215b22f77a619c/setup.py'"'"'; __file__='"'"'/private/var/folders/kn/0y92g7x55qs7c42tln4gwhtm0000gp/T/pip-install-soh30mel/mongoengine_89e68f8427244f1bb3215b22f77a619c/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/kn/0y92g7x55qs7c42tln4gwhtm0000gp/T/pip-pip-egg-info-97994d6e
9         cwd: /private/var/folders/kn/0y92g7x55qs7c42tln4gwhtm0000gp/T/pip-install-soh30mel/mongoengine_89e68f8427244f1bb3215b22f77a619c/
10    Complete output (1 lines):
11    error in mongoengine setup command: use_2to3 is invalid.
12    ----------------------------------------
13WARNING: Discarding https://*/pypi/packages/mongoengine-0.19.1.tar.gz#md5=68e613009f6466239158821a102ac084 (from https://*/pypi/simple/mongoengine/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
14ERROR: Could not find a version that satisfies the requirement mongoengine==0.19.1 (from versions: 0.15.0, 0.19.1)
15ERROR: No matching distribution found for mongoengine==0.19.1
16

ANSWER

Answered 2021-Nov-19 at 13:30

It looks like setuptools>=58 breaks support for use_2to3:

setuptools changelog for v58

So you should update setuptools to setuptools<58 or avoid using packages with use_2to3 in the setup parameters.

I was having the same problem, pip==19.3.1

Source https://stackoverflow.com/questions/69100275

QUESTION

Mapping complex JSON to Pandas Dataframe

Asked 2022-Feb-25 at 13:57

Background
I have a complex nested JSON object, which I am trying to unpack into a pandas df in a very specific way.

JSON Object
this is an extract, containing randomized data of the JSON object, which shows examples of the hierarchy (inc. children) for 1x family (i.e. 'Falconer Family'), however there is 100s of them in total and this extract just has 1x family, however the full JSON object has multiple -

1{
2    &quot;meta&quot;: {
3        &quot;columns&quot;: [{
4                &quot;key&quot;: &quot;value&quot;,
5                &quot;display_name&quot;: &quot;Adjusted Value (No Div, USD)&quot;,
6                &quot;output_type&quot;: &quot;Number&quot;,
7                &quot;currency&quot;: &quot;USD&quot;
8            },
9            {
10                &quot;key&quot;: &quot;time_weighted_return&quot;,
11                &quot;display_name&quot;: &quot;Current Quarter TWR (USD)&quot;,
12                &quot;output_type&quot;: &quot;Percent&quot;,
13                &quot;currency&quot;: &quot;USD&quot;
14            },
15            {
16                &quot;key&quot;: &quot;time_weighted_return_2&quot;,
17                &quot;display_name&quot;: &quot;YTD TWR (USD)&quot;,
18                &quot;output_type&quot;: &quot;Percent&quot;,
19                &quot;currency&quot;: &quot;USD&quot;
20            },
21            {
22                &quot;key&quot;: &quot;_custom_twr_audit_note_911328&quot;,
23                &quot;display_name&quot;: &quot;TWR Audit Note&quot;,
24                &quot;output_type&quot;: &quot;Word&quot;
25            }
26        ],
27        &quot;groupings&quot;: [{
28                &quot;key&quot;: &quot;_custom_name_747205&quot;,
29                &quot;display_name&quot;: &quot;* Reporting Client Name&quot;
30            },
31            {
32                &quot;key&quot;: &quot;_custom_new_entity_group_453577&quot;,
33                &quot;display_name&quot;: &quot;NEW Entity Group&quot;
34            },
35            {
36                &quot;key&quot;: &quot;_custom_level_2_624287&quot;,
37                &quot;display_name&quot;: &quot;* Level 2&quot;
38            },
39            {
40                &quot;key&quot;: &quot;legal_entity&quot;,
41                &quot;display_name&quot;: &quot;Legal Entity&quot;
42            }
43        ]
44    },
45    &quot;data&quot;: {
46        &quot;type&quot;: &quot;portfolio_views&quot;,
47        &quot;attributes&quot;: {
48            &quot;total&quot;: {
49                &quot;name&quot;: &quot;Total&quot;,
50                &quot;columns&quot;: {
51                    &quot;time_weighted_return&quot;: -0.046732301295604683,
52                    &quot;time_weighted_return_2&quot;: -0.046732301295604683,
53                    &quot;_custom_twr_audit_note_911328&quot;: null,
54                    &quot;value&quot;: 23132492.905107163
55                },
56                &quot;children&quot;: [{
57                    &quot;name&quot;: &quot;Falconer Family&quot;,
58                    &quot;grouping&quot;: &quot;_custom_name_747205&quot;,
59                    &quot;columns&quot;: {
60                        &quot;time_weighted_return&quot;: -0.046732301295604683,
61                        &quot;time_weighted_return_2&quot;: -0.046732301295604683,
62                        &quot;_custom_twr_audit_note_911328&quot;: null,
63                        &quot;value&quot;: 23132492.905107163
64                    },
65                    &quot;children&quot;: [{
66                            &quot;name&quot;: &quot;Wealth Bucket A&quot;,
67                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
68                            &quot;columns&quot;: {
69                                &quot;time_weighted_return&quot;: -0.045960317420568164,
70                                &quot;time_weighted_return_2&quot;: -0.045960317420568164,
71                                &quot;_custom_twr_audit_note_911328&quot;: null,
72                                &quot;value&quot;: 13264448.506587159
73                            },
74                            &quot;children&quot;: [{
75                                    &quot;name&quot;: &quot;Asset Class A&quot;,
76                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
77                                    &quot;columns&quot;: {
78                                        &quot;time_weighted_return&quot;: 0.000003434094574039648,
79                                        &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
80                                        &quot;_custom_twr_audit_note_911328&quot;: null,
81                                        &quot;value&quot;: 3337.99
82                                    },
83                                    &quot;children&quot;: [{
84                                        &quot;entity_id&quot;: 10604454,
85                                        &quot;name&quot;: &quot;HUDJ Trust&quot;,
86                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
87                                        &quot;columns&quot;: {
88                                            &quot;time_weighted_return&quot;: 0.000003434094574039648,
89                                            &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
90                                            &quot;_custom_twr_audit_note_911328&quot;: null,
91                                            &quot;value&quot;: 3337.99
92                                        },
93                                        &quot;children&quot;: []
94                                    }]
95                                },
96                                {
97                                    &quot;name&quot;: &quot;Asset Class B&quot;,
98                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
99                                    &quot;columns&quot;: {
100                                        &quot;time_weighted_return&quot;: -0.025871339096964152,
101                                        &quot;time_weighted_return_2&quot;: -0.025871339096964152,
102                                        &quot;_custom_twr_audit_note_911328&quot;: null,
103                                        &quot;value&quot;: 1017004.7192636987
104                                    },
105                                    &quot;children&quot;: [{
106                                        &quot;entity_id&quot;: 10604454,
107                                        &quot;name&quot;: &quot;HUDG Trust&quot;,
108                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
109                                        &quot;columns&quot;: {
110                                            &quot;time_weighted_return&quot;: -0.025871339096964152,
111                                            &quot;time_weighted_return_2&quot;: -0.025871339096964152,
112                                            &quot;_custom_twr_audit_note_911328&quot;: null,
113                                            &quot;value&quot;: 1017004.7192636987
114                                        },
115                                        &quot;children&quot;: []
116                                    }]
117                                },
118                                {
119                                    &quot;name&quot;: &quot;Asset Class C&quot;,
120                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
121                                    &quot;columns&quot;: {
122                                        &quot;time_weighted_return&quot;: -0.030370376329670656,
123                                        &quot;time_weighted_return_2&quot;: -0.030370376329670656,
124                                        &quot;_custom_twr_audit_note_911328&quot;: null,
125                                        &quot;value&quot;: 231142.67772000004
126                                    },
127                                    &quot;children&quot;: [{
128                                        &quot;entity_id&quot;: 10604454,
129                                        &quot;name&quot;: &quot;HKDJ Trust&quot;,
130                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
131                                        &quot;columns&quot;: {
132                                            &quot;time_weighted_return&quot;: -0.030370376329670656,
133                                            &quot;time_weighted_return_2&quot;: -0.030370376329670656,
134                                            &quot;_custom_twr_audit_note_911328&quot;: null,
135                                            &quot;value&quot;: 231142.67772000004
136                                        },
137                                        &quot;children&quot;: []
138                                    }]
139                                },
140                                {
141                                    &quot;name&quot;: &quot;Asset Class D&quot;,
142                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
143                                    &quot;columns&quot;: {
144                                        &quot;time_weighted_return&quot;: -0.05382756475465478,
145                                        &quot;time_weighted_return_2&quot;: -0.05382756475465478,
146                                        &quot;_custom_twr_audit_note_911328&quot;: null,
147                                        &quot;value&quot;: 9791282.570000006
148                                    },
149                                    &quot;children&quot;: [{
150                                        &quot;entity_id&quot;: 10604454,
151                                        &quot;name&quot;: &quot;HUDW Trust&quot;,
152                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
153                                        &quot;columns&quot;: {
154                                            &quot;time_weighted_return&quot;: -0.05382756475465478,
155                                            &quot;time_weighted_return_2&quot;: -0.05382756475465478,
156                                            &quot;_custom_twr_audit_note_911328&quot;: null,
157                                            &quot;value&quot;: 9791282.570000006
158                                        },
159                                        &quot;children&quot;: []
160                                    }]
161                                },
162                                {
163                                    &quot;name&quot;: &quot;Asset Class E&quot;,
164                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
165                                    &quot;columns&quot;: {
166                                        &quot;time_weighted_return&quot;: -0.01351630404081805,
167                                        &quot;time_weighted_return_2&quot;: -0.01351630404081805,
168                                        &quot;_custom_twr_audit_note_911328&quot;: null,
169                                        &quot;value&quot;: 2153366.6396034593
170                                    },
171                                    &quot;children&quot;: [{
172                                        &quot;entity_id&quot;: 10604454,
173                                        &quot;name&quot;: &quot;HJDJ Trust&quot;,
174                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
175                                        &quot;columns&quot;: {
176                                            &quot;time_weighted_return&quot;: -0.01351630404081805,
177                                            &quot;time_weighted_return_2&quot;: -0.01351630404081805,
178                                            &quot;_custom_twr_audit_note_911328&quot;: null,
179                                            &quot;value&quot;: 2153366.6396034593
180                                        },
181                                        &quot;children&quot;: []
182                                    }]
183                                },
184                                {
185                                    &quot;name&quot;: &quot;Asset Class F&quot;,
186                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
187                                    &quot;columns&quot;: {
188                                        &quot;time_weighted_return&quot;: -0.002298190175237247,
189                                        &quot;time_weighted_return_2&quot;: -0.002298190175237247,
190                                        &quot;_custom_twr_audit_note_911328&quot;: null,
191                                        &quot;value&quot;: 68313.90999999999
192                                    },
193                                    &quot;children&quot;: [{
194                                        &quot;entity_id&quot;: 10604454,
195                                        &quot;name&quot;: &quot;HADJ Trust&quot;,
196                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
197                                        &quot;columns&quot;: {
198                                            &quot;time_weighted_return&quot;: -0.002298190175237247,
199                                            &quot;time_weighted_return_2&quot;: -0.002298190175237247,
200                                            &quot;_custom_twr_audit_note_911328&quot;: null,
201                                            &quot;value&quot;: 68313.90999999999
202                                        },
203                                        &quot;children&quot;: []
204                                    }]
205                                }
206                            ]
207                        },
208                        {
209                            &quot;name&quot;: &quot;Wealth Bucket B&quot;,
210                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
211                            &quot;columns&quot;: {
212                                &quot;time_weighted_return&quot;: -0.04769870075659244,
213                                &quot;time_weighted_return_2&quot;: -0.04769870075659244,
214                                &quot;_custom_twr_audit_note_911328&quot;: null,
215                                &quot;value&quot;: 9868044.398519998
216                            },
217                            &quot;children&quot;: [{
218                                    &quot;name&quot;: &quot;Asset Class A&quot;,
219                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
220                                    &quot;columns&quot;: {
221                                        &quot;time_weighted_return&quot;: 0.000028632718065191298,
222                                        &quot;time_weighted_return_2&quot;: 0.000028632718065191298,
223                                        &quot;_custom_twr_audit_note_911328&quot;: null,
224                                        &quot;value&quot;: 10234.94
225                                    },
226                                    &quot;children&quot;: [{
227                                            &quot;entity_id&quot;: 10868778,
228                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
229                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
230                                            &quot;columns&quot;: {
231                                                &quot;time_weighted_return&quot;: 0.0000282679297198829,
232                                                &quot;time_weighted_return_2&quot;: 0.0000282679297198829,
233                                                &quot;_custom_twr_audit_note_911328&quot;: null,
234                                                &quot;value&quot;: 244.28
235                                            },
236                                            &quot;children&quot;: []
237                                        },
238                                        {
239                                            &quot;entity_id&quot;: 10643052,
240                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
241                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
242                                            &quot;columns&quot;: {
243                                                &quot;time_weighted_return&quot;: 0.000049373572795108345,
244                                                &quot;time_weighted_return_2&quot;: 0.000049373572795108345,
245                                                &quot;_custom_twr_audit_note_911328&quot;: null,
246                                                &quot;value&quot;: 5081.08
247                                            },
248                                            &quot;children&quot;: []
249                                        },
250                                        {
251                                            &quot;entity_id&quot;: 10598341,
252                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
253                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
254                                            &quot;columns&quot;: {
255                                                &quot;time_weighted_return&quot;: 0.000006609603754315074,
256                                                &quot;time_weighted_return_2&quot;: 0.000006609603754315074,
257                                                &quot;_custom_twr_audit_note_911328&quot;: null,
258                                                &quot;value&quot;: 1523.62
259                                            },
260                                            &quot;children&quot;: []
261                                        },
262                                        {
263                                            &quot;entity_id&quot;: 10598337,
264                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
265                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
266                                            &quot;columns&quot;: {
267                                                &quot;time_weighted_return&quot;: 0.000010999769004760296,
268                                                &quot;time_weighted_return_2&quot;: 0.000010999769004760296,
269                                                &quot;_custom_twr_audit_note_911328&quot;: null,
270                                                &quot;value&quot;: 1828.9
271                                            },
272                                            &quot;children&quot;: []
273                                        },
274                                        {
275                                            &quot;entity_id&quot;: 10598334,
276                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
277                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
278                                            &quot;columns&quot;: {
279                                                &quot;time_weighted_return&quot;: 0.000006466673995619843,
280                                                &quot;time_weighted_return_2&quot;: 0.000006466673995619843,
281                                                &quot;_custom_twr_audit_note_911328&quot;: null,
282                                                &quot;value&quot;: 1557.06
283                                            },
284                                            &quot;children&quot;: []
285                                        }
286                                    ]
287                                },
288                                {
289                                    &quot;name&quot;: &quot;Asset Class B&quot;,
290                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
291                                    &quot;columns&quot;: {
292                                        &quot;time_weighted_return&quot;: -0.024645947842438676,
293                                        &quot;time_weighted_return_2&quot;: -0.024645947842438676,
294                                        &quot;_custom_twr_audit_note_911328&quot;: null,
295                                        &quot;value&quot;: 674052.31962
296                                    },
297                                    &quot;children&quot;: [{
298                                            &quot;entity_id&quot;: 10868778,
299                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
300                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
301                                            &quot;columns&quot;: {
302                                                &quot;time_weighted_return&quot;: -0.043304004172576405,
303                                                &quot;time_weighted_return_2&quot;: -0.043304004172576405,
304                                                &quot;_custom_twr_audit_note_911328&quot;: null,
305                                                &quot;value&quot;: 52800.96
306                                            },
307                                            &quot;children&quot;: []
308                                        },
309                                        {
310                                            &quot;entity_id&quot;: 10643052,
311                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
312                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
313                                            &quot;columns&quot;: {
314                                                &quot;time_weighted_return&quot;: -0.022408434778798836,
315                                                &quot;time_weighted_return_2&quot;: -0.022408434778798836,
316                                                &quot;_custom_twr_audit_note_911328&quot;: null,
317                                                &quot;value&quot;: 599594.11962
318                                            },
319                                            &quot;children&quot;: []
320                                        },
321                                        {
322                                            &quot;entity_id&quot;: 10598341,
323                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
324                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
325                                            &quot;columns&quot;: {
326                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
327                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
328                                                &quot;_custom_twr_audit_note_911328&quot;: null,
329                                                &quot;value&quot;: 7219.08
330                                            },
331                                            &quot;children&quot;: []
332                                        },
333                                        {
334                                            &quot;entity_id&quot;: 10598337,
335                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
336                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
337                                            &quot;columns&quot;: {
338                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
339                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
340                                                &quot;_custom_twr_audit_note_911328&quot;: null,
341                                                &quot;value&quot;: 7219.08
342                                            },
343                                            &quot;children&quot;: []
344                                        },
345                                        {
346                                            &quot;entity_id&quot;: 10598334,
347                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
348                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
349                                            &quot;columns&quot;: {
350                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
351                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
352                                                &quot;_custom_twr_audit_note_911328&quot;: null,
353                                                &quot;value&quot;: 7219.08
354                                            },
355                                            &quot;children&quot;: []
356                                        }
357                                    ]
358                                },
359                                {
360                                    &quot;name&quot;: &quot;Asset Class C&quot;,
361                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
362                                    &quot;columns&quot;: {
363                                        &quot;time_weighted_return&quot;: -0.03037038746301135,
364                                        &quot;time_weighted_return_2&quot;: -0.03037038746301135,
365                                        &quot;_custom_twr_audit_note_911328&quot;: null,
366                                        &quot;value&quot;: 114472.69744
367                                    },
368                                    &quot;children&quot;: [{
369                                            &quot;entity_id&quot;: 10868778,
370                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
371                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
372                                            &quot;columns&quot;: {
373                                                &quot;time_weighted_return&quot;: -0.030370390035505124,
374                                                &quot;time_weighted_return_2&quot;: -0.030370390035505124,
375                                                &quot;_custom_twr_audit_note_911328&quot;: null,
376                                                &quot;value&quot;: 114472.68744000001
377                                            },
378                                            &quot;children&quot;: []
379                                        },
380                                        {
381                                            &quot;entity_id&quot;: 10643052,
382                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
383                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
384                                            &quot;columns&quot;: {
385                                                &quot;time_weighted_return&quot;: 0,
386                                                &quot;time_weighted_return_2&quot;: 0,
387                                                &quot;_custom_twr_audit_note_911328&quot;: null,
388                                                &quot;value&quot;: 0.01
389                                            },
390                                            &quot;children&quot;: []
391                                        }
392                                    ]
393                                },
394                                {
395                                    &quot;name&quot;: &quot;Asset Class D&quot;,
396                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
397                                    &quot;columns&quot;: {
398                                        &quot;time_weighted_return&quot;: -0.06604362523792162,
399                                        &quot;time_weighted_return_2&quot;: -0.06604362523792162,
400                                        &quot;_custom_twr_audit_note_911328&quot;: null,
401                                        &quot;value&quot;: 5722529.229999997
402                                    },
403                                    &quot;children&quot;: [{
404                                            &quot;entity_id&quot;: 10868778,
405                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
406                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
407                                            &quot;columns&quot;: {
408                                                &quot;time_weighted_return&quot;: -0.06154960593668424,
409                                                &quot;time_weighted_return_2&quot;: -0.06154960593668424,
410                                                &quot;_custom_twr_audit_note_911328&quot;: null,
411                                                &quot;value&quot;: 1191838.9399999995
412                                            },
413                                            &quot;children&quot;: []
414                                        },
415                                        {
416                                            &quot;entity_id&quot;: 10643052,
417                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
418                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
419                                            &quot;columns&quot;: {
420                                                &quot;time_weighted_return&quot;: -0.06750460387418267,
421                                                &quot;time_weighted_return_2&quot;: -0.06750460387418267,
422                                                &quot;_custom_twr_audit_note_911328&quot;: null,
423                                                &quot;value&quot;: 4416618.520000002
424                                            },
425                                            &quot;children&quot;: []
426                                        },
427                                        {
428                                            &quot;entity_id&quot;: 10598341,
429                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
430                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
431                                            &quot;columns&quot;: {
432                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
433                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
434                                                &quot;_custom_twr_audit_note_911328&quot;: null,
435                                                &quot;value&quot;: 38190.33
436                                            },
437                                            &quot;children&quot;: []
438                                        },
439                                        {
440                                            &quot;entity_id&quot;: 10598337,
441                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
442                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
443                                            &quot;columns&quot;: {
444                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
445                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
446                                                &quot;_custom_twr_audit_note_911328&quot;: null,
447                                                &quot;value&quot;: 37940.72
448                                            },
449                                            &quot;children&quot;: []
450                                        },
451                                        {
452                                            &quot;entity_id&quot;: 10598334,
453                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
454                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
455                                            &quot;columns&quot;: {
456                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
457                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
458                                                &quot;_custom_twr_audit_note_911328&quot;: null,
459                                                &quot;value&quot;: 37940.72
460                                            },
461                                            &quot;children&quot;: []
462                                        }
463                                    ]
464                                },
465                                {
466                                    &quot;name&quot;: &quot;Asset Class E&quot;,
467                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
468                                    &quot;columns&quot;: {
469                                        &quot;time_weighted_return&quot;: -0.017118805423322003,
470                                        &quot;time_weighted_return_2&quot;: -0.017118805423322003,
471                                        &quot;_custom_twr_audit_note_911328&quot;: null,
472                                        &quot;value&quot;: 3148495.0914600003
473                                    },
474                                    &quot;children&quot;: [{
475                                            &quot;entity_id&quot;: 10868778,
476                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
477                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
478                                            &quot;columns&quot;: {
479                                                &quot;time_weighted_return&quot;: -0.015251157805867277,
480                                                &quot;time_weighted_return_2&quot;: -0.015251157805867277,
481                                                &quot;_custom_twr_audit_note_911328&quot;: null,
482                                                &quot;value&quot;: 800493.06146
483                                            },
484                                            &quot;children&quot;: []
485                                        },
486                                        {
487                                            &quot;entity_id&quot;: 10643052,
488                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
489                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
490                                            &quot;columns&quot;: {
491                                                &quot;time_weighted_return&quot;: -0.01739609576880241,
492                                                &quot;time_weighted_return_2&quot;: -0.01739609576880241,
493                                                &quot;_custom_twr_audit_note_911328&quot;: null,
494                                                &quot;value&quot;: 2215511.2700000005
495                                            },
496                                            &quot;children&quot;: []
497                                        },
498                                        {
499                                            &quot;entity_id&quot;: 10598341,
500                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
501                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
502                                            &quot;columns&quot;: {
503                                                &quot;time_weighted_return&quot;: -0.02085132265594647,
504                                                &quot;time_weighted_return_2&quot;: -0.02085132265594647,
505                                                &quot;_custom_twr_audit_note_911328&quot;: null,
506                                                &quot;value&quot;: 44031.21
507                                            },
508                                            &quot;children&quot;: []
509                                        },
510                                        {
511                                            &quot;entity_id&quot;: 10598337,
512                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
513                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
514                                            &quot;columns&quot;: {
515                                                &quot;time_weighted_return&quot;: -0.02089393244695803,
516                                                &quot;time_weighted_return_2&quot;: -0.02089393244695803,
517                                                &quot;_custom_twr_audit_note_911328&quot;: null,
518                                                &quot;value&quot;: 44394.159999999996
519                                            },
520                                            &quot;children&quot;: []
521                                        },
522                                        {
523                                            &quot;entity_id&quot;: 10598334,
524                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
525                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
526                                            &quot;columns&quot;: {
527                                                &quot;time_weighted_return&quot;: -0.020607507059866248,
528                                                &quot;time_weighted_return_2&quot;: -0.020607507059866248,
529                                                &quot;_custom_twr_audit_note_911328&quot;: null,
530                                                &quot;value&quot;: 44065.39000000001
531                                            },
532                                            &quot;children&quot;: []
533                                        }
534                                    ]
535                                },
536                                {
537                                    &quot;name&quot;: &quot;Asset Class F&quot;,
538                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
539                                    &quot;columns&quot;: {
540                                        &quot;time_weighted_return&quot;: -0.0014710489231547497,
541                                        &quot;time_weighted_return_2&quot;: -0.0014710489231547497,
542                                        &quot;_custom_twr_audit_note_911328&quot;: null,
543                                        &quot;value&quot;: 198260.12
544                                    },
545                                    &quot;children&quot;: [{
546                                            &quot;entity_id&quot;: 10868778,
547                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
548                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
549                                            &quot;columns&quot;: {
550                                                &quot;time_weighted_return&quot;: -0.0014477244560456848,
551                                                &quot;time_weighted_return_2&quot;: -0.0014477244560456848,
552                                                &quot;_custom_twr_audit_note_911328&quot;: null,
553                                                &quot;value&quot;: 44612.33
554                                            },
555                                            &quot;children&quot;: []
556                                        },
557                                        {
558                                            &quot;entity_id&quot;: 10643052,
559                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
560                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
561                                            &quot;columns&quot;: {
562                                                &quot;time_weighted_return&quot;: -0.001477821083437858,
563                                                &quot;time_weighted_return_2&quot;: -0.001477821083437858,
564                                                &quot;_custom_twr_audit_note_911328&quot;: null,
565                                                &quot;value&quot;: 153647.78999999998
566                                            },
567                                            &quot;children&quot;: []
568                                        }
569                                    ]
570                                }
571                            ]
572                        }
573                    ]
574                }]
575            }
576        },
577        &quot;included&quot;: []
578    }
579}
580

Notes on JSON Object extract

  1. data - data in here can be ignored, these are aggregated values for underlying children.
  2. meta - columns – contains the column header values I want to use for each applicable children ‘column` key:pair values.
  3. groupings - can be ignored.
  4. children hierarchy – there are 4x levels of children which can be identified by their name as follows –
    • Family name (i.e., ‘Falconer Family’)
    • Wealth Bucket name (e.g., ‘Wealth Bucket A’)
    • Asset Class name (e.g., ‘Asset Class A’)
    • Fund name (e.g., ‘HUDJ Trust’)

Target Output
this is an extract of target df structure I am trying to achieve -

portfolio name entity_id Adjusted Value (No Div, USD) Current Quarter TWR (USD) YTD TWR (USD) TWR Audit Note
Falconer Family Falconer Family 23132492.90510712 -0.046732301295604683 -0.046732301295604683 None
Falconer Family Wealth Bucket A 13264448.506587146 -0.045960317420568164 -0.045960317420568164 None
Falconer Family Asset Class A 3337.99 0.000003434094574039648 0.000003434094574039648 None
Falconer Family HUDJ Trust 10604454 3337.99 0.000003434094574039648 0.000003434094574039648 None
Falconer Family Asset Class B 1017004.7192636987 -0.025871339096964152 -0.025871339096964152 None
Falconer Family HUDG Trust 10604454 1017004.7192636987 -0.025871339096964152 -0.025871339096964152 None
Falconer Family Asset Class C 231142.67772000004 -0.030370376329670656 -0.030370376329670656 None
Falconer Family HKDJ Trust 10604454 231142.67772000004 -0.030370376329670656 -0.030370376329670656 None
Falconer Family Asset Class D 9791282.570000006 -0.05382756475465478 -0.05382756475465478 None
Falconer Family HUDW Trust 10604454 9791282.570000006 -0.05382756475465478 -0.05382756475465478 None

Notes on Target Output

  1. Portfolio header – for every row, I would like to map the top-level children name value [family name]. E.g., ‘Falconer Family.
  2. Name header – this should simply be the name value from each respective children.
  3. Entity ID – all 4th level children entity_id value should be mapped to this column.
  4. Data columns – regardless of level, all children have identical time_weighted_return, time-weighted_return2 and value columns which should be mapped respectively.
  5. TWR Audit Note – these children _custom_twr_audit_note_911318 values are currently blank, but will be utilized in the future.

Current Output
My main issue is that you can see that I have only been able to tap into the 1st [Family] and 2nd [Wealth Bucket] children level. This leaves me missing the 3rd [Asset Class] and 4th [Fund] -

portfolio name Adjusted Value (No Div, USD) Current Quarter TWR (USD) YTD TWR (USD) TWR Audit Note)
0 Falconer Family Falconer Family 2.313249e+07 -0.046732 -0.046732 None
1 Falconer Family Wealth Bucket A 1.326445e+07 -0.045960 -0.045960 None
2 Falconer Family Wealth Bucket B 9.868044e+06 -0.047699 -0.047699 None

Current code
This is a function which gets me the correct df formatting, however my main issue is that I haven't been able to find a solution to returning all children, but rather only the top-level -

1{
2    &quot;meta&quot;: {
3        &quot;columns&quot;: [{
4                &quot;key&quot;: &quot;value&quot;,
5                &quot;display_name&quot;: &quot;Adjusted Value (No Div, USD)&quot;,
6                &quot;output_type&quot;: &quot;Number&quot;,
7                &quot;currency&quot;: &quot;USD&quot;
8            },
9            {
10                &quot;key&quot;: &quot;time_weighted_return&quot;,
11                &quot;display_name&quot;: &quot;Current Quarter TWR (USD)&quot;,
12                &quot;output_type&quot;: &quot;Percent&quot;,
13                &quot;currency&quot;: &quot;USD&quot;
14            },
15            {
16                &quot;key&quot;: &quot;time_weighted_return_2&quot;,
17                &quot;display_name&quot;: &quot;YTD TWR (USD)&quot;,
18                &quot;output_type&quot;: &quot;Percent&quot;,
19                &quot;currency&quot;: &quot;USD&quot;
20            },
21            {
22                &quot;key&quot;: &quot;_custom_twr_audit_note_911328&quot;,
23                &quot;display_name&quot;: &quot;TWR Audit Note&quot;,
24                &quot;output_type&quot;: &quot;Word&quot;
25            }
26        ],
27        &quot;groupings&quot;: [{
28                &quot;key&quot;: &quot;_custom_name_747205&quot;,
29                &quot;display_name&quot;: &quot;* Reporting Client Name&quot;
30            },
31            {
32                &quot;key&quot;: &quot;_custom_new_entity_group_453577&quot;,
33                &quot;display_name&quot;: &quot;NEW Entity Group&quot;
34            },
35            {
36                &quot;key&quot;: &quot;_custom_level_2_624287&quot;,
37                &quot;display_name&quot;: &quot;* Level 2&quot;
38            },
39            {
40                &quot;key&quot;: &quot;legal_entity&quot;,
41                &quot;display_name&quot;: &quot;Legal Entity&quot;
42            }
43        ]
44    },
45    &quot;data&quot;: {
46        &quot;type&quot;: &quot;portfolio_views&quot;,
47        &quot;attributes&quot;: {
48            &quot;total&quot;: {
49                &quot;name&quot;: &quot;Total&quot;,
50                &quot;columns&quot;: {
51                    &quot;time_weighted_return&quot;: -0.046732301295604683,
52                    &quot;time_weighted_return_2&quot;: -0.046732301295604683,
53                    &quot;_custom_twr_audit_note_911328&quot;: null,
54                    &quot;value&quot;: 23132492.905107163
55                },
56                &quot;children&quot;: [{
57                    &quot;name&quot;: &quot;Falconer Family&quot;,
58                    &quot;grouping&quot;: &quot;_custom_name_747205&quot;,
59                    &quot;columns&quot;: {
60                        &quot;time_weighted_return&quot;: -0.046732301295604683,
61                        &quot;time_weighted_return_2&quot;: -0.046732301295604683,
62                        &quot;_custom_twr_audit_note_911328&quot;: null,
63                        &quot;value&quot;: 23132492.905107163
64                    },
65                    &quot;children&quot;: [{
66                            &quot;name&quot;: &quot;Wealth Bucket A&quot;,
67                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
68                            &quot;columns&quot;: {
69                                &quot;time_weighted_return&quot;: -0.045960317420568164,
70                                &quot;time_weighted_return_2&quot;: -0.045960317420568164,
71                                &quot;_custom_twr_audit_note_911328&quot;: null,
72                                &quot;value&quot;: 13264448.506587159
73                            },
74                            &quot;children&quot;: [{
75                                    &quot;name&quot;: &quot;Asset Class A&quot;,
76                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
77                                    &quot;columns&quot;: {
78                                        &quot;time_weighted_return&quot;: 0.000003434094574039648,
79                                        &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
80                                        &quot;_custom_twr_audit_note_911328&quot;: null,
81                                        &quot;value&quot;: 3337.99
82                                    },
83                                    &quot;children&quot;: [{
84                                        &quot;entity_id&quot;: 10604454,
85                                        &quot;name&quot;: &quot;HUDJ Trust&quot;,
86                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
87                                        &quot;columns&quot;: {
88                                            &quot;time_weighted_return&quot;: 0.000003434094574039648,
89                                            &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
90                                            &quot;_custom_twr_audit_note_911328&quot;: null,
91                                            &quot;value&quot;: 3337.99
92                                        },
93                                        &quot;children&quot;: []
94                                    }]
95                                },
96                                {
97                                    &quot;name&quot;: &quot;Asset Class B&quot;,
98                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
99                                    &quot;columns&quot;: {
100                                        &quot;time_weighted_return&quot;: -0.025871339096964152,
101                                        &quot;time_weighted_return_2&quot;: -0.025871339096964152,
102                                        &quot;_custom_twr_audit_note_911328&quot;: null,
103                                        &quot;value&quot;: 1017004.7192636987
104                                    },
105                                    &quot;children&quot;: [{
106                                        &quot;entity_id&quot;: 10604454,
107                                        &quot;name&quot;: &quot;HUDG Trust&quot;,
108                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
109                                        &quot;columns&quot;: {
110                                            &quot;time_weighted_return&quot;: -0.025871339096964152,
111                                            &quot;time_weighted_return_2&quot;: -0.025871339096964152,
112                                            &quot;_custom_twr_audit_note_911328&quot;: null,
113                                            &quot;value&quot;: 1017004.7192636987
114                                        },
115                                        &quot;children&quot;: []
116                                    }]
117                                },
118                                {
119                                    &quot;name&quot;: &quot;Asset Class C&quot;,
120                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
121                                    &quot;columns&quot;: {
122                                        &quot;time_weighted_return&quot;: -0.030370376329670656,
123                                        &quot;time_weighted_return_2&quot;: -0.030370376329670656,
124                                        &quot;_custom_twr_audit_note_911328&quot;: null,
125                                        &quot;value&quot;: 231142.67772000004
126                                    },
127                                    &quot;children&quot;: [{
128                                        &quot;entity_id&quot;: 10604454,
129                                        &quot;name&quot;: &quot;HKDJ Trust&quot;,
130                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
131                                        &quot;columns&quot;: {
132                                            &quot;time_weighted_return&quot;: -0.030370376329670656,
133                                            &quot;time_weighted_return_2&quot;: -0.030370376329670656,
134                                            &quot;_custom_twr_audit_note_911328&quot;: null,
135                                            &quot;value&quot;: 231142.67772000004
136                                        },
137                                        &quot;children&quot;: []
138                                    }]
139                                },
140                                {
141                                    &quot;name&quot;: &quot;Asset Class D&quot;,
142                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
143                                    &quot;columns&quot;: {
144                                        &quot;time_weighted_return&quot;: -0.05382756475465478,
145                                        &quot;time_weighted_return_2&quot;: -0.05382756475465478,
146                                        &quot;_custom_twr_audit_note_911328&quot;: null,
147                                        &quot;value&quot;: 9791282.570000006
148                                    },
149                                    &quot;children&quot;: [{
150                                        &quot;entity_id&quot;: 10604454,
151                                        &quot;name&quot;: &quot;HUDW Trust&quot;,
152                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
153                                        &quot;columns&quot;: {
154                                            &quot;time_weighted_return&quot;: -0.05382756475465478,
155                                            &quot;time_weighted_return_2&quot;: -0.05382756475465478,
156                                            &quot;_custom_twr_audit_note_911328&quot;: null,
157                                            &quot;value&quot;: 9791282.570000006
158                                        },
159                                        &quot;children&quot;: []
160                                    }]
161                                },
162                                {
163                                    &quot;name&quot;: &quot;Asset Class E&quot;,
164                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
165                                    &quot;columns&quot;: {
166                                        &quot;time_weighted_return&quot;: -0.01351630404081805,
167                                        &quot;time_weighted_return_2&quot;: -0.01351630404081805,
168                                        &quot;_custom_twr_audit_note_911328&quot;: null,
169                                        &quot;value&quot;: 2153366.6396034593
170                                    },
171                                    &quot;children&quot;: [{
172                                        &quot;entity_id&quot;: 10604454,
173                                        &quot;name&quot;: &quot;HJDJ Trust&quot;,
174                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
175                                        &quot;columns&quot;: {
176                                            &quot;time_weighted_return&quot;: -0.01351630404081805,
177                                            &quot;time_weighted_return_2&quot;: -0.01351630404081805,
178                                            &quot;_custom_twr_audit_note_911328&quot;: null,
179                                            &quot;value&quot;: 2153366.6396034593
180                                        },
181                                        &quot;children&quot;: []
182                                    }]
183                                },
184                                {
185                                    &quot;name&quot;: &quot;Asset Class F&quot;,
186                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
187                                    &quot;columns&quot;: {
188                                        &quot;time_weighted_return&quot;: -0.002298190175237247,
189                                        &quot;time_weighted_return_2&quot;: -0.002298190175237247,
190                                        &quot;_custom_twr_audit_note_911328&quot;: null,
191                                        &quot;value&quot;: 68313.90999999999
192                                    },
193                                    &quot;children&quot;: [{
194                                        &quot;entity_id&quot;: 10604454,
195                                        &quot;name&quot;: &quot;HADJ Trust&quot;,
196                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
197                                        &quot;columns&quot;: {
198                                            &quot;time_weighted_return&quot;: -0.002298190175237247,
199                                            &quot;time_weighted_return_2&quot;: -0.002298190175237247,
200                                            &quot;_custom_twr_audit_note_911328&quot;: null,
201                                            &quot;value&quot;: 68313.90999999999
202                                        },
203                                        &quot;children&quot;: []
204                                    }]
205                                }
206                            ]
207                        },
208                        {
209                            &quot;name&quot;: &quot;Wealth Bucket B&quot;,
210                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
211                            &quot;columns&quot;: {
212                                &quot;time_weighted_return&quot;: -0.04769870075659244,
213                                &quot;time_weighted_return_2&quot;: -0.04769870075659244,
214                                &quot;_custom_twr_audit_note_911328&quot;: null,
215                                &quot;value&quot;: 9868044.398519998
216                            },
217                            &quot;children&quot;: [{
218                                    &quot;name&quot;: &quot;Asset Class A&quot;,
219                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
220                                    &quot;columns&quot;: {
221                                        &quot;time_weighted_return&quot;: 0.000028632718065191298,
222                                        &quot;time_weighted_return_2&quot;: 0.000028632718065191298,
223                                        &quot;_custom_twr_audit_note_911328&quot;: null,
224                                        &quot;value&quot;: 10234.94
225                                    },
226                                    &quot;children&quot;: [{
227                                            &quot;entity_id&quot;: 10868778,
228                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
229                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
230                                            &quot;columns&quot;: {
231                                                &quot;time_weighted_return&quot;: 0.0000282679297198829,
232                                                &quot;time_weighted_return_2&quot;: 0.0000282679297198829,
233                                                &quot;_custom_twr_audit_note_911328&quot;: null,
234                                                &quot;value&quot;: 244.28
235                                            },
236                                            &quot;children&quot;: []
237                                        },
238                                        {
239                                            &quot;entity_id&quot;: 10643052,
240                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
241                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
242                                            &quot;columns&quot;: {
243                                                &quot;time_weighted_return&quot;: 0.000049373572795108345,
244                                                &quot;time_weighted_return_2&quot;: 0.000049373572795108345,
245                                                &quot;_custom_twr_audit_note_911328&quot;: null,
246                                                &quot;value&quot;: 5081.08
247                                            },
248                                            &quot;children&quot;: []
249                                        },
250                                        {
251                                            &quot;entity_id&quot;: 10598341,
252                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
253                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
254                                            &quot;columns&quot;: {
255                                                &quot;time_weighted_return&quot;: 0.000006609603754315074,
256                                                &quot;time_weighted_return_2&quot;: 0.000006609603754315074,
257                                                &quot;_custom_twr_audit_note_911328&quot;: null,
258                                                &quot;value&quot;: 1523.62
259                                            },
260                                            &quot;children&quot;: []
261                                        },
262                                        {
263                                            &quot;entity_id&quot;: 10598337,
264                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
265                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
266                                            &quot;columns&quot;: {
267                                                &quot;time_weighted_return&quot;: 0.000010999769004760296,
268                                                &quot;time_weighted_return_2&quot;: 0.000010999769004760296,
269                                                &quot;_custom_twr_audit_note_911328&quot;: null,
270                                                &quot;value&quot;: 1828.9
271                                            },
272                                            &quot;children&quot;: []
273                                        },
274                                        {
275                                            &quot;entity_id&quot;: 10598334,
276                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
277                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
278                                            &quot;columns&quot;: {
279                                                &quot;time_weighted_return&quot;: 0.000006466673995619843,
280                                                &quot;time_weighted_return_2&quot;: 0.000006466673995619843,
281                                                &quot;_custom_twr_audit_note_911328&quot;: null,
282                                                &quot;value&quot;: 1557.06
283                                            },
284                                            &quot;children&quot;: []
285                                        }
286                                    ]
287                                },
288                                {
289                                    &quot;name&quot;: &quot;Asset Class B&quot;,
290                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
291                                    &quot;columns&quot;: {
292                                        &quot;time_weighted_return&quot;: -0.024645947842438676,
293                                        &quot;time_weighted_return_2&quot;: -0.024645947842438676,
294                                        &quot;_custom_twr_audit_note_911328&quot;: null,
295                                        &quot;value&quot;: 674052.31962
296                                    },
297                                    &quot;children&quot;: [{
298                                            &quot;entity_id&quot;: 10868778,
299                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
300                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
301                                            &quot;columns&quot;: {
302                                                &quot;time_weighted_return&quot;: -0.043304004172576405,
303                                                &quot;time_weighted_return_2&quot;: -0.043304004172576405,
304                                                &quot;_custom_twr_audit_note_911328&quot;: null,
305                                                &quot;value&quot;: 52800.96
306                                            },
307                                            &quot;children&quot;: []
308                                        },
309                                        {
310                                            &quot;entity_id&quot;: 10643052,
311                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
312                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
313                                            &quot;columns&quot;: {
314                                                &quot;time_weighted_return&quot;: -0.022408434778798836,
315                                                &quot;time_weighted_return_2&quot;: -0.022408434778798836,
316                                                &quot;_custom_twr_audit_note_911328&quot;: null,
317                                                &quot;value&quot;: 599594.11962
318                                            },
319                                            &quot;children&quot;: []
320                                        },
321                                        {
322                                            &quot;entity_id&quot;: 10598341,
323                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
324                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
325                                            &quot;columns&quot;: {
326                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
327                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
328                                                &quot;_custom_twr_audit_note_911328&quot;: null,
329                                                &quot;value&quot;: 7219.08
330                                            },
331                                            &quot;children&quot;: []
332                                        },
333                                        {
334                                            &quot;entity_id&quot;: 10598337,
335                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
336                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
337                                            &quot;columns&quot;: {
338                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
339                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
340                                                &quot;_custom_twr_audit_note_911328&quot;: null,
341                                                &quot;value&quot;: 7219.08
342                                            },
343                                            &quot;children&quot;: []
344                                        },
345                                        {
346                                            &quot;entity_id&quot;: 10598334,
347                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
348                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
349                                            &quot;columns&quot;: {
350                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
351                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
352                                                &quot;_custom_twr_audit_note_911328&quot;: null,
353                                                &quot;value&quot;: 7219.08
354                                            },
355                                            &quot;children&quot;: []
356                                        }
357                                    ]
358                                },
359                                {
360                                    &quot;name&quot;: &quot;Asset Class C&quot;,
361                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
362                                    &quot;columns&quot;: {
363                                        &quot;time_weighted_return&quot;: -0.03037038746301135,
364                                        &quot;time_weighted_return_2&quot;: -0.03037038746301135,
365                                        &quot;_custom_twr_audit_note_911328&quot;: null,
366                                        &quot;value&quot;: 114472.69744
367                                    },
368                                    &quot;children&quot;: [{
369                                            &quot;entity_id&quot;: 10868778,
370                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
371                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
372                                            &quot;columns&quot;: {
373                                                &quot;time_weighted_return&quot;: -0.030370390035505124,
374                                                &quot;time_weighted_return_2&quot;: -0.030370390035505124,
375                                                &quot;_custom_twr_audit_note_911328&quot;: null,
376                                                &quot;value&quot;: 114472.68744000001
377                                            },
378                                            &quot;children&quot;: []
379                                        },
380                                        {
381                                            &quot;entity_id&quot;: 10643052,
382                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
383                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
384                                            &quot;columns&quot;: {
385                                                &quot;time_weighted_return&quot;: 0,
386                                                &quot;time_weighted_return_2&quot;: 0,
387                                                &quot;_custom_twr_audit_note_911328&quot;: null,
388                                                &quot;value&quot;: 0.01
389                                            },
390                                            &quot;children&quot;: []
391                                        }
392                                    ]
393                                },
394                                {
395                                    &quot;name&quot;: &quot;Asset Class D&quot;,
396                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
397                                    &quot;columns&quot;: {
398                                        &quot;time_weighted_return&quot;: -0.06604362523792162,
399                                        &quot;time_weighted_return_2&quot;: -0.06604362523792162,
400                                        &quot;_custom_twr_audit_note_911328&quot;: null,
401                                        &quot;value&quot;: 5722529.229999997
402                                    },
403                                    &quot;children&quot;: [{
404                                            &quot;entity_id&quot;: 10868778,
405                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
406                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
407                                            &quot;columns&quot;: {
408                                                &quot;time_weighted_return&quot;: -0.06154960593668424,
409                                                &quot;time_weighted_return_2&quot;: -0.06154960593668424,
410                                                &quot;_custom_twr_audit_note_911328&quot;: null,
411                                                &quot;value&quot;: 1191838.9399999995
412                                            },
413                                            &quot;children&quot;: []
414                                        },
415                                        {
416                                            &quot;entity_id&quot;: 10643052,
417                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
418                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
419                                            &quot;columns&quot;: {
420                                                &quot;time_weighted_return&quot;: -0.06750460387418267,
421                                                &quot;time_weighted_return_2&quot;: -0.06750460387418267,
422                                                &quot;_custom_twr_audit_note_911328&quot;: null,
423                                                &quot;value&quot;: 4416618.520000002
424                                            },
425                                            &quot;children&quot;: []
426                                        },
427                                        {
428                                            &quot;entity_id&quot;: 10598341,
429                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
430                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
431                                            &quot;columns&quot;: {
432                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
433                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
434                                                &quot;_custom_twr_audit_note_911328&quot;: null,
435                                                &quot;value&quot;: 38190.33
436                                            },
437                                            &quot;children&quot;: []
438                                        },
439                                        {
440                                            &quot;entity_id&quot;: 10598337,
441                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
442                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
443                                            &quot;columns&quot;: {
444                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
445                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
446                                                &quot;_custom_twr_audit_note_911328&quot;: null,
447                                                &quot;value&quot;: 37940.72
448                                            },
449                                            &quot;children&quot;: []
450                                        },
451                                        {
452                                            &quot;entity_id&quot;: 10598334,
453                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
454                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
455                                            &quot;columns&quot;: {
456                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
457                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
458                                                &quot;_custom_twr_audit_note_911328&quot;: null,
459                                                &quot;value&quot;: 37940.72
460                                            },
461                                            &quot;children&quot;: []
462                                        }
463                                    ]
464                                },
465                                {
466                                    &quot;name&quot;: &quot;Asset Class E&quot;,
467                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
468                                    &quot;columns&quot;: {
469                                        &quot;time_weighted_return&quot;: -0.017118805423322003,
470                                        &quot;time_weighted_return_2&quot;: -0.017118805423322003,
471                                        &quot;_custom_twr_audit_note_911328&quot;: null,
472                                        &quot;value&quot;: 3148495.0914600003
473                                    },
474                                    &quot;children&quot;: [{
475                                            &quot;entity_id&quot;: 10868778,
476                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
477                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
478                                            &quot;columns&quot;: {
479                                                &quot;time_weighted_return&quot;: -0.015251157805867277,
480                                                &quot;time_weighted_return_2&quot;: -0.015251157805867277,
481                                                &quot;_custom_twr_audit_note_911328&quot;: null,
482                                                &quot;value&quot;: 800493.06146
483                                            },
484                                            &quot;children&quot;: []
485                                        },
486                                        {
487                                            &quot;entity_id&quot;: 10643052,
488                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
489                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
490                                            &quot;columns&quot;: {
491                                                &quot;time_weighted_return&quot;: -0.01739609576880241,
492                                                &quot;time_weighted_return_2&quot;: -0.01739609576880241,
493                                                &quot;_custom_twr_audit_note_911328&quot;: null,
494                                                &quot;value&quot;: 2215511.2700000005
495                                            },
496                                            &quot;children&quot;: []
497                                        },
498                                        {
499                                            &quot;entity_id&quot;: 10598341,
500                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
501                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
502                                            &quot;columns&quot;: {
503                                                &quot;time_weighted_return&quot;: -0.02085132265594647,
504                                                &quot;time_weighted_return_2&quot;: -0.02085132265594647,
505                                                &quot;_custom_twr_audit_note_911328&quot;: null,
506                                                &quot;value&quot;: 44031.21
507                                            },
508                                            &quot;children&quot;: []
509                                        },
510                                        {
511                                            &quot;entity_id&quot;: 10598337,
512                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
513                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
514                                            &quot;columns&quot;: {
515                                                &quot;time_weighted_return&quot;: -0.02089393244695803,
516                                                &quot;time_weighted_return_2&quot;: -0.02089393244695803,
517                                                &quot;_custom_twr_audit_note_911328&quot;: null,
518                                                &quot;value&quot;: 44394.159999999996
519                                            },
520                                            &quot;children&quot;: []
521                                        },
522                                        {
523                                            &quot;entity_id&quot;: 10598334,
524                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
525                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
526                                            &quot;columns&quot;: {
527                                                &quot;time_weighted_return&quot;: -0.020607507059866248,
528                                                &quot;time_weighted_return_2&quot;: -0.020607507059866248,
529                                                &quot;_custom_twr_audit_note_911328&quot;: null,
530                                                &quot;value&quot;: 44065.39000000001
531                                            },
532                                            &quot;children&quot;: []
533                                        }
534                                    ]
535                                },
536                                {
537                                    &quot;name&quot;: &quot;Asset Class F&quot;,
538                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
539                                    &quot;columns&quot;: {
540                                        &quot;time_weighted_return&quot;: -0.0014710489231547497,
541                                        &quot;time_weighted_return_2&quot;: -0.0014710489231547497,
542                                        &quot;_custom_twr_audit_note_911328&quot;: null,
543                                        &quot;value&quot;: 198260.12
544                                    },
545                                    &quot;children&quot;: [{
546                                            &quot;entity_id&quot;: 10868778,
547                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
548                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
549                                            &quot;columns&quot;: {
550                                                &quot;time_weighted_return&quot;: -0.0014477244560456848,
551                                                &quot;time_weighted_return_2&quot;: -0.0014477244560456848,
552                                                &quot;_custom_twr_audit_note_911328&quot;: null,
553                                                &quot;value&quot;: 44612.33
554                                            },
555                                            &quot;children&quot;: []
556                                        },
557                                        {
558                                            &quot;entity_id&quot;: 10643052,
559                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
560                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
561                                            &quot;columns&quot;: {
562                                                &quot;time_weighted_return&quot;: -0.001477821083437858,
563                                                &quot;time_weighted_return_2&quot;: -0.001477821083437858,
564                                                &quot;_custom_twr_audit_note_911328&quot;: null,
565                                                &quot;value&quot;: 153647.78999999998
566                                            },
567                                            &quot;children&quot;: []
568                                        }
569                                    ]
570                                }
571                            ]
572                        }
573                    ]
574                }]
575            }
576        },
577        &quot;included&quot;: []
578    }
579}
580# Function to read API response / JSON Object
581def response_writer():
582    with open('api_response_2022-02-13.json') as f:
583        api_response = json.load(f)
584        return api_response
585
586# Function to unpack JSON response into pandas dataframe.
587def unpack_response():
588    while True:
589        try:
590            api_response = response_writer()
591            portfolio_views_children = api_response['data']['attributes']['total']['children']
592            portfolios = []
593            for portfolio in portfolio_views_children:
594                entity_columns = []
595                # include portfolio itself within an iterable so the total is the header
596                for entity in itertools.chain([portfolio], portfolio[&quot;children&quot;]):
597                    entity_data = entity[&quot;columns&quot;].copy()  # don't mutate original response
598                    entity_data[&quot;portfolio&quot;] = portfolio[&quot;name&quot;]   # from outer
599                    entity_data[&quot;name&quot;]      = entity[&quot;name&quot;]
600                    entity_columns.append(entity_data)
601
602                df = pd.DataFrame(entity_columns)
603                portfolios.append(df)
604
605            # combine dataframes
606            df = pd.concat(portfolios)
607            # reorder and rename
608            column_ordering = {&quot;portfolio&quot;: &quot;portfolio&quot;, &quot;name&quot;: &quot;name&quot;}
609            column_ordering.update({c[&quot;key&quot;]: c[&quot;display_name&quot;] for c in api_response[&quot;meta&quot;][&quot;columns&quot;]})
610            df = df[column_ordering.keys()]   # beware: un-named cols will be dropped
611            df = df.rename(columns=column_ordering)
612            break
613        except KeyError:
614            print(&quot;-----------------------------------\n&quot;,&quot;API TIMEOUT ERROR: TRY AGAIN...&quot;, &quot;\n-----------------------------------\n&quot;)
615    return df
616unpack_response()
617

Help
In short, I am looking for some advice on how I can tap into the remaining children by enhancing the existing code. Whilst I have taken much time to fully explain my problem, please ask if anything isn't clear. Please note that the JSON may have multiple families, so the solution / advice offered must observe this

ANSWER

Answered 2022-Feb-16 at 06:41

I think this gets you pretty close; might just need to adjust the various name columns and drop the extra data (I kept the grouping column).

The main idea is to recursively use pd.json_normalize with pd.concat for all availalable children levels.

EDIT: Put everything into a single function and added section to collapse the name columns like the expected output.

1{
2    &quot;meta&quot;: {
3        &quot;columns&quot;: [{
4                &quot;key&quot;: &quot;value&quot;,
5                &quot;display_name&quot;: &quot;Adjusted Value (No Div, USD)&quot;,
6                &quot;output_type&quot;: &quot;Number&quot;,
7                &quot;currency&quot;: &quot;USD&quot;
8            },
9            {
10                &quot;key&quot;: &quot;time_weighted_return&quot;,
11                &quot;display_name&quot;: &quot;Current Quarter TWR (USD)&quot;,
12                &quot;output_type&quot;: &quot;Percent&quot;,
13                &quot;currency&quot;: &quot;USD&quot;
14            },
15            {
16                &quot;key&quot;: &quot;time_weighted_return_2&quot;,
17                &quot;display_name&quot;: &quot;YTD TWR (USD)&quot;,
18                &quot;output_type&quot;: &quot;Percent&quot;,
19                &quot;currency&quot;: &quot;USD&quot;
20            },
21            {
22                &quot;key&quot;: &quot;_custom_twr_audit_note_911328&quot;,
23                &quot;display_name&quot;: &quot;TWR Audit Note&quot;,
24                &quot;output_type&quot;: &quot;Word&quot;
25            }
26        ],
27        &quot;groupings&quot;: [{
28                &quot;key&quot;: &quot;_custom_name_747205&quot;,
29                &quot;display_name&quot;: &quot;* Reporting Client Name&quot;
30            },
31            {
32                &quot;key&quot;: &quot;_custom_new_entity_group_453577&quot;,
33                &quot;display_name&quot;: &quot;NEW Entity Group&quot;
34            },
35            {
36                &quot;key&quot;: &quot;_custom_level_2_624287&quot;,
37                &quot;display_name&quot;: &quot;* Level 2&quot;
38            },
39            {
40                &quot;key&quot;: &quot;legal_entity&quot;,
41                &quot;display_name&quot;: &quot;Legal Entity&quot;
42            }
43        ]
44    },
45    &quot;data&quot;: {
46        &quot;type&quot;: &quot;portfolio_views&quot;,
47        &quot;attributes&quot;: {
48            &quot;total&quot;: {
49                &quot;name&quot;: &quot;Total&quot;,
50                &quot;columns&quot;: {
51                    &quot;time_weighted_return&quot;: -0.046732301295604683,
52                    &quot;time_weighted_return_2&quot;: -0.046732301295604683,
53                    &quot;_custom_twr_audit_note_911328&quot;: null,
54                    &quot;value&quot;: 23132492.905107163
55                },
56                &quot;children&quot;: [{
57                    &quot;name&quot;: &quot;Falconer Family&quot;,
58                    &quot;grouping&quot;: &quot;_custom_name_747205&quot;,
59                    &quot;columns&quot;: {
60                        &quot;time_weighted_return&quot;: -0.046732301295604683,
61                        &quot;time_weighted_return_2&quot;: -0.046732301295604683,
62                        &quot;_custom_twr_audit_note_911328&quot;: null,
63                        &quot;value&quot;: 23132492.905107163
64                    },
65                    &quot;children&quot;: [{
66                            &quot;name&quot;: &quot;Wealth Bucket A&quot;,
67                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
68                            &quot;columns&quot;: {
69                                &quot;time_weighted_return&quot;: -0.045960317420568164,
70                                &quot;time_weighted_return_2&quot;: -0.045960317420568164,
71                                &quot;_custom_twr_audit_note_911328&quot;: null,
72                                &quot;value&quot;: 13264448.506587159
73                            },
74                            &quot;children&quot;: [{
75                                    &quot;name&quot;: &quot;Asset Class A&quot;,
76                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
77                                    &quot;columns&quot;: {
78                                        &quot;time_weighted_return&quot;: 0.000003434094574039648,
79                                        &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
80                                        &quot;_custom_twr_audit_note_911328&quot;: null,
81                                        &quot;value&quot;: 3337.99
82                                    },
83                                    &quot;children&quot;: [{
84                                        &quot;entity_id&quot;: 10604454,
85                                        &quot;name&quot;: &quot;HUDJ Trust&quot;,
86                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
87                                        &quot;columns&quot;: {
88                                            &quot;time_weighted_return&quot;: 0.000003434094574039648,
89                                            &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
90                                            &quot;_custom_twr_audit_note_911328&quot;: null,
91                                            &quot;value&quot;: 3337.99
92                                        },
93                                        &quot;children&quot;: []
94                                    }]
95                                },
96                                {
97                                    &quot;name&quot;: &quot;Asset Class B&quot;,
98                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
99                                    &quot;columns&quot;: {
100                                        &quot;time_weighted_return&quot;: -0.025871339096964152,
101                                        &quot;time_weighted_return_2&quot;: -0.025871339096964152,
102                                        &quot;_custom_twr_audit_note_911328&quot;: null,
103                                        &quot;value&quot;: 1017004.7192636987
104                                    },
105                                    &quot;children&quot;: [{
106                                        &quot;entity_id&quot;: 10604454,
107                                        &quot;name&quot;: &quot;HUDG Trust&quot;,
108                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
109                                        &quot;columns&quot;: {
110                                            &quot;time_weighted_return&quot;: -0.025871339096964152,
111                                            &quot;time_weighted_return_2&quot;: -0.025871339096964152,
112                                            &quot;_custom_twr_audit_note_911328&quot;: null,
113                                            &quot;value&quot;: 1017004.7192636987
114                                        },
115                                        &quot;children&quot;: []
116                                    }]
117                                },
118                                {
119                                    &quot;name&quot;: &quot;Asset Class C&quot;,
120                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
121                                    &quot;columns&quot;: {
122                                        &quot;time_weighted_return&quot;: -0.030370376329670656,
123                                        &quot;time_weighted_return_2&quot;: -0.030370376329670656,
124                                        &quot;_custom_twr_audit_note_911328&quot;: null,
125                                        &quot;value&quot;: 231142.67772000004
126                                    },
127                                    &quot;children&quot;: [{
128                                        &quot;entity_id&quot;: 10604454,
129                                        &quot;name&quot;: &quot;HKDJ Trust&quot;,
130                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
131                                        &quot;columns&quot;: {
132                                            &quot;time_weighted_return&quot;: -0.030370376329670656,
133                                            &quot;time_weighted_return_2&quot;: -0.030370376329670656,
134                                            &quot;_custom_twr_audit_note_911328&quot;: null,
135                                            &quot;value&quot;: 231142.67772000004
136                                        },
137                                        &quot;children&quot;: []
138                                    }]
139                                },
140                                {
141                                    &quot;name&quot;: &quot;Asset Class D&quot;,
142                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
143                                    &quot;columns&quot;: {
144                                        &quot;time_weighted_return&quot;: -0.05382756475465478,
145                                        &quot;time_weighted_return_2&quot;: -0.05382756475465478,
146                                        &quot;_custom_twr_audit_note_911328&quot;: null,
147                                        &quot;value&quot;: 9791282.570000006
148                                    },
149                                    &quot;children&quot;: [{
150                                        &quot;entity_id&quot;: 10604454,
151                                        &quot;name&quot;: &quot;HUDW Trust&quot;,
152                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
153                                        &quot;columns&quot;: {
154                                            &quot;time_weighted_return&quot;: -0.05382756475465478,
155                                            &quot;time_weighted_return_2&quot;: -0.05382756475465478,
156                                            &quot;_custom_twr_audit_note_911328&quot;: null,
157                                            &quot;value&quot;: 9791282.570000006
158                                        },
159                                        &quot;children&quot;: []
160                                    }]
161                                },
162                                {
163                                    &quot;name&quot;: &quot;Asset Class E&quot;,
164                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
165                                    &quot;columns&quot;: {
166                                        &quot;time_weighted_return&quot;: -0.01351630404081805,
167                                        &quot;time_weighted_return_2&quot;: -0.01351630404081805,
168                                        &quot;_custom_twr_audit_note_911328&quot;: null,
169                                        &quot;value&quot;: 2153366.6396034593
170                                    },
171                                    &quot;children&quot;: [{
172                                        &quot;entity_id&quot;: 10604454,
173                                        &quot;name&quot;: &quot;HJDJ Trust&quot;,
174                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
175                                        &quot;columns&quot;: {
176                                            &quot;time_weighted_return&quot;: -0.01351630404081805,
177                                            &quot;time_weighted_return_2&quot;: -0.01351630404081805,
178                                            &quot;_custom_twr_audit_note_911328&quot;: null,
179                                            &quot;value&quot;: 2153366.6396034593
180                                        },
181                                        &quot;children&quot;: []
182                                    }]
183                                },
184                                {
185                                    &quot;name&quot;: &quot;Asset Class F&quot;,
186                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
187                                    &quot;columns&quot;: {
188                                        &quot;time_weighted_return&quot;: -0.002298190175237247,
189                                        &quot;time_weighted_return_2&quot;: -0.002298190175237247,
190                                        &quot;_custom_twr_audit_note_911328&quot;: null,
191                                        &quot;value&quot;: 68313.90999999999
192                                    },
193                                    &quot;children&quot;: [{
194                                        &quot;entity_id&quot;: 10604454,
195                                        &quot;name&quot;: &quot;HADJ Trust&quot;,
196                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
197                                        &quot;columns&quot;: {
198                                            &quot;time_weighted_return&quot;: -0.002298190175237247,
199                                            &quot;time_weighted_return_2&quot;: -0.002298190175237247,
200                                            &quot;_custom_twr_audit_note_911328&quot;: null,
201                                            &quot;value&quot;: 68313.90999999999
202                                        },
203                                        &quot;children&quot;: []
204                                    }]
205                                }
206                            ]
207                        },
208                        {
209                            &quot;name&quot;: &quot;Wealth Bucket B&quot;,
210                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
211                            &quot;columns&quot;: {
212                                &quot;time_weighted_return&quot;: -0.04769870075659244,
213                                &quot;time_weighted_return_2&quot;: -0.04769870075659244,
214                                &quot;_custom_twr_audit_note_911328&quot;: null,
215                                &quot;value&quot;: 9868044.398519998
216                            },
217                            &quot;children&quot;: [{
218                                    &quot;name&quot;: &quot;Asset Class A&quot;,
219                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
220                                    &quot;columns&quot;: {
221                                        &quot;time_weighted_return&quot;: 0.000028632718065191298,
222                                        &quot;time_weighted_return_2&quot;: 0.000028632718065191298,
223                                        &quot;_custom_twr_audit_note_911328&quot;: null,
224                                        &quot;value&quot;: 10234.94
225                                    },
226                                    &quot;children&quot;: [{
227                                            &quot;entity_id&quot;: 10868778,
228                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
229                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
230                                            &quot;columns&quot;: {
231                                                &quot;time_weighted_return&quot;: 0.0000282679297198829,
232                                                &quot;time_weighted_return_2&quot;: 0.0000282679297198829,
233                                                &quot;_custom_twr_audit_note_911328&quot;: null,
234                                                &quot;value&quot;: 244.28
235                                            },
236                                            &quot;children&quot;: []
237                                        },
238                                        {
239                                            &quot;entity_id&quot;: 10643052,
240                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
241                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
242                                            &quot;columns&quot;: {
243                                                &quot;time_weighted_return&quot;: 0.000049373572795108345,
244                                                &quot;time_weighted_return_2&quot;: 0.000049373572795108345,
245                                                &quot;_custom_twr_audit_note_911328&quot;: null,
246                                                &quot;value&quot;: 5081.08
247                                            },
248                                            &quot;children&quot;: []
249                                        },
250                                        {
251                                            &quot;entity_id&quot;: 10598341,
252                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
253                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
254                                            &quot;columns&quot;: {
255                                                &quot;time_weighted_return&quot;: 0.000006609603754315074,
256                                                &quot;time_weighted_return_2&quot;: 0.000006609603754315074,
257                                                &quot;_custom_twr_audit_note_911328&quot;: null,
258                                                &quot;value&quot;: 1523.62
259                                            },
260                                            &quot;children&quot;: []
261                                        },
262                                        {
263                                            &quot;entity_id&quot;: 10598337,
264                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
265                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
266                                            &quot;columns&quot;: {
267                                                &quot;time_weighted_return&quot;: 0.000010999769004760296,
268                                                &quot;time_weighted_return_2&quot;: 0.000010999769004760296,
269                                                &quot;_custom_twr_audit_note_911328&quot;: null,
270                                                &quot;value&quot;: 1828.9
271                                            },
272                                            &quot;children&quot;: []
273                                        },
274                                        {
275                                            &quot;entity_id&quot;: 10598334,
276                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
277                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
278                                            &quot;columns&quot;: {
279                                                &quot;time_weighted_return&quot;: 0.000006466673995619843,
280                                                &quot;time_weighted_return_2&quot;: 0.000006466673995619843,
281                                                &quot;_custom_twr_audit_note_911328&quot;: null,
282                                                &quot;value&quot;: 1557.06
283                                            },
284                                            &quot;children&quot;: []
285                                        }
286                                    ]
287                                },
288                                {
289                                    &quot;name&quot;: &quot;Asset Class B&quot;,
290                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
291                                    &quot;columns&quot;: {
292                                        &quot;time_weighted_return&quot;: -0.024645947842438676,
293                                        &quot;time_weighted_return_2&quot;: -0.024645947842438676,
294                                        &quot;_custom_twr_audit_note_911328&quot;: null,
295                                        &quot;value&quot;: 674052.31962
296                                    },
297                                    &quot;children&quot;: [{
298                                            &quot;entity_id&quot;: 10868778,
299                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
300                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
301                                            &quot;columns&quot;: {
302                                                &quot;time_weighted_return&quot;: -0.043304004172576405,
303                                                &quot;time_weighted_return_2&quot;: -0.043304004172576405,
304                                                &quot;_custom_twr_audit_note_911328&quot;: null,
305                                                &quot;value&quot;: 52800.96
306                                            },
307                                            &quot;children&quot;: []
308                                        },
309                                        {
310                                            &quot;entity_id&quot;: 10643052,
311                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
312                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
313                                            &quot;columns&quot;: {
314                                                &quot;time_weighted_return&quot;: -0.022408434778798836,
315                                                &quot;time_weighted_return_2&quot;: -0.022408434778798836,
316                                                &quot;_custom_twr_audit_note_911328&quot;: null,
317                                                &quot;value&quot;: 599594.11962
318                                            },
319                                            &quot;children&quot;: []
320                                        },
321                                        {
322                                            &quot;entity_id&quot;: 10598341,
323                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
324                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
325                                            &quot;columns&quot;: {
326                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
327                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
328                                                &quot;_custom_twr_audit_note_911328&quot;: null,
329                                                &quot;value&quot;: 7219.08
330                                            },
331                                            &quot;children&quot;: []
332                                        },
333                                        {
334                                            &quot;entity_id&quot;: 10598337,
335                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
336                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
337                                            &quot;columns&quot;: {
338                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
339                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
340                                                &quot;_custom_twr_audit_note_911328&quot;: null,
341                                                &quot;value&quot;: 7219.08
342                                            },
343                                            &quot;children&quot;: []
344                                        },
345                                        {
346                                            &quot;entity_id&quot;: 10598334,
347                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
348                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
349                                            &quot;columns&quot;: {
350                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
351                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
352                                                &quot;_custom_twr_audit_note_911328&quot;: null,
353                                                &quot;value&quot;: 7219.08
354                                            },
355                                            &quot;children&quot;: []
356                                        }
357                                    ]
358                                },
359                                {
360                                    &quot;name&quot;: &quot;Asset Class C&quot;,
361                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
362                                    &quot;columns&quot;: {
363                                        &quot;time_weighted_return&quot;: -0.03037038746301135,
364                                        &quot;time_weighted_return_2&quot;: -0.03037038746301135,
365                                        &quot;_custom_twr_audit_note_911328&quot;: null,
366                                        &quot;value&quot;: 114472.69744
367                                    },
368                                    &quot;children&quot;: [{
369                                            &quot;entity_id&quot;: 10868778,
370                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
371                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
372                                            &quot;columns&quot;: {
373                                                &quot;time_weighted_return&quot;: -0.030370390035505124,
374                                                &quot;time_weighted_return_2&quot;: -0.030370390035505124,
375                                                &quot;_custom_twr_audit_note_911328&quot;: null,
376                                                &quot;value&quot;: 114472.68744000001
377                                            },
378                                            &quot;children&quot;: []
379                                        },
380                                        {
381                                            &quot;entity_id&quot;: 10643052,
382                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
383                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
384                                            &quot;columns&quot;: {
385                                                &quot;time_weighted_return&quot;: 0,
386                                                &quot;time_weighted_return_2&quot;: 0,
387                                                &quot;_custom_twr_audit_note_911328&quot;: null,
388                                                &quot;value&quot;: 0.01
389                                            },
390                                            &quot;children&quot;: []
391                                        }
392                                    ]
393                                },
394                                {
395                                    &quot;name&quot;: &quot;Asset Class D&quot;,
396                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
397                                    &quot;columns&quot;: {
398                                        &quot;time_weighted_return&quot;: -0.06604362523792162,
399                                        &quot;time_weighted_return_2&quot;: -0.06604362523792162,
400                                        &quot;_custom_twr_audit_note_911328&quot;: null,
401                                        &quot;value&quot;: 5722529.229999997
402                                    },
403                                    &quot;children&quot;: [{
404                                            &quot;entity_id&quot;: 10868778,
405                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
406                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
407                                            &quot;columns&quot;: {
408                                                &quot;time_weighted_return&quot;: -0.06154960593668424,
409                                                &quot;time_weighted_return_2&quot;: -0.06154960593668424,
410                                                &quot;_custom_twr_audit_note_911328&quot;: null,
411                                                &quot;value&quot;: 1191838.9399999995
412                                            },
413                                            &quot;children&quot;: []
414                                        },
415                                        {
416                                            &quot;entity_id&quot;: 10643052,
417                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
418                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
419                                            &quot;columns&quot;: {
420                                                &quot;time_weighted_return&quot;: -0.06750460387418267,
421                                                &quot;time_weighted_return_2&quot;: -0.06750460387418267,
422                                                &quot;_custom_twr_audit_note_911328&quot;: null,
423                                                &quot;value&quot;: 4416618.520000002
424                                            },
425                                            &quot;children&quot;: []
426                                        },
427                                        {
428                                            &quot;entity_id&quot;: 10598341,
429                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
430                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
431                                            &quot;columns&quot;: {
432                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
433                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
434                                                &quot;_custom_twr_audit_note_911328&quot;: null,
435                                                &quot;value&quot;: 38190.33
436                                            },
437                                            &quot;children&quot;: []
438                                        },
439                                        {
440                                            &quot;entity_id&quot;: 10598337,
441                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
442                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
443                                            &quot;columns&quot;: {
444                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
445                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
446                                                &quot;_custom_twr_audit_note_911328&quot;: null,
447                                                &quot;value&quot;: 37940.72
448                                            },
449                                            &quot;children&quot;: []
450                                        },
451                                        {
452                                            &quot;entity_id&quot;: 10598334,
453                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
454                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
455                                            &quot;columns&quot;: {
456                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
457                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
458                                                &quot;_custom_twr_audit_note_911328&quot;: null,
459                                                &quot;value&quot;: 37940.72
460                                            },
461                                            &quot;children&quot;: []
462                                        }
463                                    ]
464                                },
465                                {
466                                    &quot;name&quot;: &quot;Asset Class E&quot;,
467                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
468                                    &quot;columns&quot;: {
469                                        &quot;time_weighted_return&quot;: -0.017118805423322003,
470                                        &quot;time_weighted_return_2&quot;: -0.017118805423322003,
471                                        &quot;_custom_twr_audit_note_911328&quot;: null,
472                                        &quot;value&quot;: 3148495.0914600003
473                                    },
474                                    &quot;children&quot;: [{
475                                            &quot;entity_id&quot;: 10868778,
476                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
477                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
478                                            &quot;columns&quot;: {
479                                                &quot;time_weighted_return&quot;: -0.015251157805867277,
480                                                &quot;time_weighted_return_2&quot;: -0.015251157805867277,
481                                                &quot;_custom_twr_audit_note_911328&quot;: null,
482                                                &quot;value&quot;: 800493.06146
483                                            },
484                                            &quot;children&quot;: []
485                                        },
486                                        {
487                                            &quot;entity_id&quot;: 10643052,
488                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
489                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
490                                            &quot;columns&quot;: {
491                                                &quot;time_weighted_return&quot;: -0.01739609576880241,
492                                                &quot;time_weighted_return_2&quot;: -0.01739609576880241,
493                                                &quot;_custom_twr_audit_note_911328&quot;: null,
494                                                &quot;value&quot;: 2215511.2700000005
495                                            },
496                                            &quot;children&quot;: []
497                                        },
498                                        {
499                                            &quot;entity_id&quot;: 10598341,
500                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
501                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
502                                            &quot;columns&quot;: {
503                                                &quot;time_weighted_return&quot;: -0.02085132265594647,
504                                                &quot;time_weighted_return_2&quot;: -0.02085132265594647,
505                                                &quot;_custom_twr_audit_note_911328&quot;: null,
506                                                &quot;value&quot;: 44031.21
507                                            },
508                                            &quot;children&quot;: []
509                                        },
510                                        {
511                                            &quot;entity_id&quot;: 10598337,
512                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
513                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
514                                            &quot;columns&quot;: {
515                                                &quot;time_weighted_return&quot;: -0.02089393244695803,
516                                                &quot;time_weighted_return_2&quot;: -0.02089393244695803,
517                                                &quot;_custom_twr_audit_note_911328&quot;: null,
518                                                &quot;value&quot;: 44394.159999999996
519                                            },
520                                            &quot;children&quot;: []
521                                        },
522                                        {
523                                            &quot;entity_id&quot;: 10598334,
524                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
525                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
526                                            &quot;columns&quot;: {
527                                                &quot;time_weighted_return&quot;: -0.020607507059866248,
528                                                &quot;time_weighted_return_2&quot;: -0.020607507059866248,
529                                                &quot;_custom_twr_audit_note_911328&quot;: null,
530                                                &quot;value&quot;: 44065.39000000001
531                                            },
532                                            &quot;children&quot;: []
533                                        }
534                                    ]
535                                },
536                                {
537                                    &quot;name&quot;: &quot;Asset Class F&quot;,
538                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
539                                    &quot;columns&quot;: {
540                                        &quot;time_weighted_return&quot;: -0.0014710489231547497,
541                                        &quot;time_weighted_return_2&quot;: -0.0014710489231547497,
542                                        &quot;_custom_twr_audit_note_911328&quot;: null,
543                                        &quot;value&quot;: 198260.12
544                                    },
545                                    &quot;children&quot;: [{
546                                            &quot;entity_id&quot;: 10868778,
547                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
548                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
549                                            &quot;columns&quot;: {
550                                                &quot;time_weighted_return&quot;: -0.0014477244560456848,
551                                                &quot;time_weighted_return_2&quot;: -0.0014477244560456848,
552                                                &quot;_custom_twr_audit_note_911328&quot;: null,
553                                                &quot;value&quot;: 44612.33
554                                            },
555                                            &quot;children&quot;: []
556                                        },
557                                        {
558                                            &quot;entity_id&quot;: 10643052,
559                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
560                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
561                                            &quot;columns&quot;: {
562                                                &quot;time_weighted_return&quot;: -0.001477821083437858,
563                                                &quot;time_weighted_return_2&quot;: -0.001477821083437858,
564                                                &quot;_custom_twr_audit_note_911328&quot;: null,
565                                                &quot;value&quot;: 153647.78999999998
566                                            },
567                                            &quot;children&quot;: []
568                                        }
569                                    ]
570                                }
571                            ]
572                        }
573                    ]
574                }]
575            }
576        },
577        &quot;included&quot;: []
578    }
579}
580# Function to read API response / JSON Object
581def response_writer():
582    with open('api_response_2022-02-13.json') as f:
583        api_response = json.load(f)
584        return api_response
585
586# Function to unpack JSON response into pandas dataframe.
587def unpack_response():
588    while True:
589        try:
590            api_response = response_writer()
591            portfolio_views_children = api_response['data']['attributes']['total']['children']
592            portfolios = []
593            for portfolio in portfolio_views_children:
594                entity_columns = []
595                # include portfolio itself within an iterable so the total is the header
596                for entity in itertools.chain([portfolio], portfolio[&quot;children&quot;]):
597                    entity_data = entity[&quot;columns&quot;].copy()  # don't mutate original response
598                    entity_data[&quot;portfolio&quot;] = portfolio[&quot;name&quot;]   # from outer
599                    entity_data[&quot;name&quot;]      = entity[&quot;name&quot;]
600                    entity_columns.append(entity_data)
601
602                df = pd.DataFrame(entity_columns)
603                portfolios.append(df)
604
605            # combine dataframes
606            df = pd.concat(portfolios)
607            # reorder and rename
608            column_ordering = {&quot;portfolio&quot;: &quot;portfolio&quot;, &quot;name&quot;: &quot;name&quot;}
609            column_ordering.update({c[&quot;key&quot;]: c[&quot;display_name&quot;] for c in api_response[&quot;meta&quot;][&quot;columns&quot;]})
610            df = df[column_ordering.keys()]   # beware: un-named cols will be dropped
611            df = df.rename(columns=column_ordering)
612            break
613        except KeyError:
614            print(&quot;-----------------------------------\n&quot;,&quot;API TIMEOUT ERROR: TRY AGAIN...&quot;, &quot;\n-----------------------------------\n&quot;)
615    return df
616unpack_response()
617def process_json(api_response): 
618    
619    def get_column_values(df):    
620        return pd.concat([df, pd.json_normalize(df.pop('columns')).set_axis(df.index)], axis=1)
621    
622    def expand_children(df):
623        if len(df.index) &gt; 1:
624            df['children'] = df['children'].fillna('').apply(lambda x: None if len(x) == 0 else x)
625        df_children = df.pop('children').dropna().explode()
626        if len(df_children.index) == 0: # return df if no children to append
627            return df.index.names, df
628        df_children = pd.json_normalize(df_children, max_level=0).set_axis(df_children.index).set_index('name', append=True)
629        df_children = get_column_values(df_children)
630        idx_names = list(df_children.index.names)
631        idx_names[-1] = idx_names[-1] + '_' + str(len(idx_names))
632        df[idx_names[-1]] = None
633        return idx_names, pd.concat([df.set_index(idx_names[-1], append=True), df_children], axis=0)    
634    
635    columns_dict = pd.DataFrame(api_response['meta']['columns']).set_index('key').to_dict(orient='index') # save column definitions
636    df = pd.DataFrame(api_response['data']['attributes']['total']['children']).set_index('name') # get initial dataframe     
637    df = get_column_values(df) # get columns for initial level
638    
639    # expand children
640    while 'children' in df.columns:
641        idx_names, df = expand_children(df)
642    
643    # reorder/replace column headers and sort index
644    df = (df.loc[:, [x for x in df.columns if x not in columns_dict.keys()] + list(columns_dict.keys())]
645          .rename(columns={k:v['display_name'] for k,v in columns_dict.items()})
646          .sort_index(na_position='first').reset_index())
647    
648    #collapse &quot;name&quot; columns (careful of potential duplicate rows)  
649    for col in idx_names[::-1]:
650            df[idx_names[-1]] = df[idx_names[-1]].fillna(df[col])
651    df = df.rename(columns={'name': 'portfolio', idx_names[-1]: 'name'}).drop(columns=idx_names[1:-1])      
652    
653    return df
654

Since the other answer uses iterrows, which usually isn't advised, figured a quick time compare was worthwhile.

1{
2    &quot;meta&quot;: {
3        &quot;columns&quot;: [{
4                &quot;key&quot;: &quot;value&quot;,
5                &quot;display_name&quot;: &quot;Adjusted Value (No Div, USD)&quot;,
6                &quot;output_type&quot;: &quot;Number&quot;,
7                &quot;currency&quot;: &quot;USD&quot;
8            },
9            {
10                &quot;key&quot;: &quot;time_weighted_return&quot;,
11                &quot;display_name&quot;: &quot;Current Quarter TWR (USD)&quot;,
12                &quot;output_type&quot;: &quot;Percent&quot;,
13                &quot;currency&quot;: &quot;USD&quot;
14            },
15            {
16                &quot;key&quot;: &quot;time_weighted_return_2&quot;,
17                &quot;display_name&quot;: &quot;YTD TWR (USD)&quot;,
18                &quot;output_type&quot;: &quot;Percent&quot;,
19                &quot;currency&quot;: &quot;USD&quot;
20            },
21            {
22                &quot;key&quot;: &quot;_custom_twr_audit_note_911328&quot;,
23                &quot;display_name&quot;: &quot;TWR Audit Note&quot;,
24                &quot;output_type&quot;: &quot;Word&quot;
25            }
26        ],
27        &quot;groupings&quot;: [{
28                &quot;key&quot;: &quot;_custom_name_747205&quot;,
29                &quot;display_name&quot;: &quot;* Reporting Client Name&quot;
30            },
31            {
32                &quot;key&quot;: &quot;_custom_new_entity_group_453577&quot;,
33                &quot;display_name&quot;: &quot;NEW Entity Group&quot;
34            },
35            {
36                &quot;key&quot;: &quot;_custom_level_2_624287&quot;,
37                &quot;display_name&quot;: &quot;* Level 2&quot;
38            },
39            {
40                &quot;key&quot;: &quot;legal_entity&quot;,
41                &quot;display_name&quot;: &quot;Legal Entity&quot;
42            }
43        ]
44    },
45    &quot;data&quot;: {
46        &quot;type&quot;: &quot;portfolio_views&quot;,
47        &quot;attributes&quot;: {
48            &quot;total&quot;: {
49                &quot;name&quot;: &quot;Total&quot;,
50                &quot;columns&quot;: {
51                    &quot;time_weighted_return&quot;: -0.046732301295604683,
52                    &quot;time_weighted_return_2&quot;: -0.046732301295604683,
53                    &quot;_custom_twr_audit_note_911328&quot;: null,
54                    &quot;value&quot;: 23132492.905107163
55                },
56                &quot;children&quot;: [{
57                    &quot;name&quot;: &quot;Falconer Family&quot;,
58                    &quot;grouping&quot;: &quot;_custom_name_747205&quot;,
59                    &quot;columns&quot;: {
60                        &quot;time_weighted_return&quot;: -0.046732301295604683,
61                        &quot;time_weighted_return_2&quot;: -0.046732301295604683,
62                        &quot;_custom_twr_audit_note_911328&quot;: null,
63                        &quot;value&quot;: 23132492.905107163
64                    },
65                    &quot;children&quot;: [{
66                            &quot;name&quot;: &quot;Wealth Bucket A&quot;,
67                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
68                            &quot;columns&quot;: {
69                                &quot;time_weighted_return&quot;: -0.045960317420568164,
70                                &quot;time_weighted_return_2&quot;: -0.045960317420568164,
71                                &quot;_custom_twr_audit_note_911328&quot;: null,
72                                &quot;value&quot;: 13264448.506587159
73                            },
74                            &quot;children&quot;: [{
75                                    &quot;name&quot;: &quot;Asset Class A&quot;,
76                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
77                                    &quot;columns&quot;: {
78                                        &quot;time_weighted_return&quot;: 0.000003434094574039648,
79                                        &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
80                                        &quot;_custom_twr_audit_note_911328&quot;: null,
81                                        &quot;value&quot;: 3337.99
82                                    },
83                                    &quot;children&quot;: [{
84                                        &quot;entity_id&quot;: 10604454,
85                                        &quot;name&quot;: &quot;HUDJ Trust&quot;,
86                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
87                                        &quot;columns&quot;: {
88                                            &quot;time_weighted_return&quot;: 0.000003434094574039648,
89                                            &quot;time_weighted_return_2&quot;: 0.000003434094574039648,
90                                            &quot;_custom_twr_audit_note_911328&quot;: null,
91                                            &quot;value&quot;: 3337.99
92                                        },
93                                        &quot;children&quot;: []
94                                    }]
95                                },
96                                {
97                                    &quot;name&quot;: &quot;Asset Class B&quot;,
98                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
99                                    &quot;columns&quot;: {
100                                        &quot;time_weighted_return&quot;: -0.025871339096964152,
101                                        &quot;time_weighted_return_2&quot;: -0.025871339096964152,
102                                        &quot;_custom_twr_audit_note_911328&quot;: null,
103                                        &quot;value&quot;: 1017004.7192636987
104                                    },
105                                    &quot;children&quot;: [{
106                                        &quot;entity_id&quot;: 10604454,
107                                        &quot;name&quot;: &quot;HUDG Trust&quot;,
108                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
109                                        &quot;columns&quot;: {
110                                            &quot;time_weighted_return&quot;: -0.025871339096964152,
111                                            &quot;time_weighted_return_2&quot;: -0.025871339096964152,
112                                            &quot;_custom_twr_audit_note_911328&quot;: null,
113                                            &quot;value&quot;: 1017004.7192636987
114                                        },
115                                        &quot;children&quot;: []
116                                    }]
117                                },
118                                {
119                                    &quot;name&quot;: &quot;Asset Class C&quot;,
120                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
121                                    &quot;columns&quot;: {
122                                        &quot;time_weighted_return&quot;: -0.030370376329670656,
123                                        &quot;time_weighted_return_2&quot;: -0.030370376329670656,
124                                        &quot;_custom_twr_audit_note_911328&quot;: null,
125                                        &quot;value&quot;: 231142.67772000004
126                                    },
127                                    &quot;children&quot;: [{
128                                        &quot;entity_id&quot;: 10604454,
129                                        &quot;name&quot;: &quot;HKDJ Trust&quot;,
130                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
131                                        &quot;columns&quot;: {
132                                            &quot;time_weighted_return&quot;: -0.030370376329670656,
133                                            &quot;time_weighted_return_2&quot;: -0.030370376329670656,
134                                            &quot;_custom_twr_audit_note_911328&quot;: null,
135                                            &quot;value&quot;: 231142.67772000004
136                                        },
137                                        &quot;children&quot;: []
138                                    }]
139                                },
140                                {
141                                    &quot;name&quot;: &quot;Asset Class D&quot;,
142                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
143                                    &quot;columns&quot;: {
144                                        &quot;time_weighted_return&quot;: -0.05382756475465478,
145                                        &quot;time_weighted_return_2&quot;: -0.05382756475465478,
146                                        &quot;_custom_twr_audit_note_911328&quot;: null,
147                                        &quot;value&quot;: 9791282.570000006
148                                    },
149                                    &quot;children&quot;: [{
150                                        &quot;entity_id&quot;: 10604454,
151                                        &quot;name&quot;: &quot;HUDW Trust&quot;,
152                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
153                                        &quot;columns&quot;: {
154                                            &quot;time_weighted_return&quot;: -0.05382756475465478,
155                                            &quot;time_weighted_return_2&quot;: -0.05382756475465478,
156                                            &quot;_custom_twr_audit_note_911328&quot;: null,
157                                            &quot;value&quot;: 9791282.570000006
158                                        },
159                                        &quot;children&quot;: []
160                                    }]
161                                },
162                                {
163                                    &quot;name&quot;: &quot;Asset Class E&quot;,
164                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
165                                    &quot;columns&quot;: {
166                                        &quot;time_weighted_return&quot;: -0.01351630404081805,
167                                        &quot;time_weighted_return_2&quot;: -0.01351630404081805,
168                                        &quot;_custom_twr_audit_note_911328&quot;: null,
169                                        &quot;value&quot;: 2153366.6396034593
170                                    },
171                                    &quot;children&quot;: [{
172                                        &quot;entity_id&quot;: 10604454,
173                                        &quot;name&quot;: &quot;HJDJ Trust&quot;,
174                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
175                                        &quot;columns&quot;: {
176                                            &quot;time_weighted_return&quot;: -0.01351630404081805,
177                                            &quot;time_weighted_return_2&quot;: -0.01351630404081805,
178                                            &quot;_custom_twr_audit_note_911328&quot;: null,
179                                            &quot;value&quot;: 2153366.6396034593
180                                        },
181                                        &quot;children&quot;: []
182                                    }]
183                                },
184                                {
185                                    &quot;name&quot;: &quot;Asset Class F&quot;,
186                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
187                                    &quot;columns&quot;: {
188                                        &quot;time_weighted_return&quot;: -0.002298190175237247,
189                                        &quot;time_weighted_return_2&quot;: -0.002298190175237247,
190                                        &quot;_custom_twr_audit_note_911328&quot;: null,
191                                        &quot;value&quot;: 68313.90999999999
192                                    },
193                                    &quot;children&quot;: [{
194                                        &quot;entity_id&quot;: 10604454,
195                                        &quot;name&quot;: &quot;HADJ Trust&quot;,
196                                        &quot;grouping&quot;: &quot;legal_entity&quot;,
197                                        &quot;columns&quot;: {
198                                            &quot;time_weighted_return&quot;: -0.002298190175237247,
199                                            &quot;time_weighted_return_2&quot;: -0.002298190175237247,
200                                            &quot;_custom_twr_audit_note_911328&quot;: null,
201                                            &quot;value&quot;: 68313.90999999999
202                                        },
203                                        &quot;children&quot;: []
204                                    }]
205                                }
206                            ]
207                        },
208                        {
209                            &quot;name&quot;: &quot;Wealth Bucket B&quot;,
210                            &quot;grouping&quot;: &quot;_custom_new_entity_group_453577&quot;,
211                            &quot;columns&quot;: {
212                                &quot;time_weighted_return&quot;: -0.04769870075659244,
213                                &quot;time_weighted_return_2&quot;: -0.04769870075659244,
214                                &quot;_custom_twr_audit_note_911328&quot;: null,
215                                &quot;value&quot;: 9868044.398519998
216                            },
217                            &quot;children&quot;: [{
218                                    &quot;name&quot;: &quot;Asset Class A&quot;,
219                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
220                                    &quot;columns&quot;: {
221                                        &quot;time_weighted_return&quot;: 0.000028632718065191298,
222                                        &quot;time_weighted_return_2&quot;: 0.000028632718065191298,
223                                        &quot;_custom_twr_audit_note_911328&quot;: null,
224                                        &quot;value&quot;: 10234.94
225                                    },
226                                    &quot;children&quot;: [{
227                                            &quot;entity_id&quot;: 10868778,
228                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
229                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
230                                            &quot;columns&quot;: {
231                                                &quot;time_weighted_return&quot;: 0.0000282679297198829,
232                                                &quot;time_weighted_return_2&quot;: 0.0000282679297198829,
233                                                &quot;_custom_twr_audit_note_911328&quot;: null,
234                                                &quot;value&quot;: 244.28
235                                            },
236                                            &quot;children&quot;: []
237                                        },
238                                        {
239                                            &quot;entity_id&quot;: 10643052,
240                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
241                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
242                                            &quot;columns&quot;: {
243                                                &quot;time_weighted_return&quot;: 0.000049373572795108345,
244                                                &quot;time_weighted_return_2&quot;: 0.000049373572795108345,
245                                                &quot;_custom_twr_audit_note_911328&quot;: null,
246                                                &quot;value&quot;: 5081.08
247                                            },
248                                            &quot;children&quot;: []
249                                        },
250                                        {
251                                            &quot;entity_id&quot;: 10598341,
252                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
253                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
254                                            &quot;columns&quot;: {
255                                                &quot;time_weighted_return&quot;: 0.000006609603754315074,
256                                                &quot;time_weighted_return_2&quot;: 0.000006609603754315074,
257                                                &quot;_custom_twr_audit_note_911328&quot;: null,
258                                                &quot;value&quot;: 1523.62
259                                            },
260                                            &quot;children&quot;: []
261                                        },
262                                        {
263                                            &quot;entity_id&quot;: 10598337,
264                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
265                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
266                                            &quot;columns&quot;: {
267                                                &quot;time_weighted_return&quot;: 0.000010999769004760296,
268                                                &quot;time_weighted_return_2&quot;: 0.000010999769004760296,
269                                                &quot;_custom_twr_audit_note_911328&quot;: null,
270                                                &quot;value&quot;: 1828.9
271                                            },
272                                            &quot;children&quot;: []
273                                        },
274                                        {
275                                            &quot;entity_id&quot;: 10598334,
276                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
277                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
278                                            &quot;columns&quot;: {
279                                                &quot;time_weighted_return&quot;: 0.000006466673995619843,
280                                                &quot;time_weighted_return_2&quot;: 0.000006466673995619843,
281                                                &quot;_custom_twr_audit_note_911328&quot;: null,
282                                                &quot;value&quot;: 1557.06
283                                            },
284                                            &quot;children&quot;: []
285                                        }
286                                    ]
287                                },
288                                {
289                                    &quot;name&quot;: &quot;Asset Class B&quot;,
290                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
291                                    &quot;columns&quot;: {
292                                        &quot;time_weighted_return&quot;: -0.024645947842438676,
293                                        &quot;time_weighted_return_2&quot;: -0.024645947842438676,
294                                        &quot;_custom_twr_audit_note_911328&quot;: null,
295                                        &quot;value&quot;: 674052.31962
296                                    },
297                                    &quot;children&quot;: [{
298                                            &quot;entity_id&quot;: 10868778,
299                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
300                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
301                                            &quot;columns&quot;: {
302                                                &quot;time_weighted_return&quot;: -0.043304004172576405,
303                                                &quot;time_weighted_return_2&quot;: -0.043304004172576405,
304                                                &quot;_custom_twr_audit_note_911328&quot;: null,
305                                                &quot;value&quot;: 52800.96
306                                            },
307                                            &quot;children&quot;: []
308                                        },
309                                        {
310                                            &quot;entity_id&quot;: 10643052,
311                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
312                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
313                                            &quot;columns&quot;: {
314                                                &quot;time_weighted_return&quot;: -0.022408434778798836,
315                                                &quot;time_weighted_return_2&quot;: -0.022408434778798836,
316                                                &quot;_custom_twr_audit_note_911328&quot;: null,
317                                                &quot;value&quot;: 599594.11962
318                                            },
319                                            &quot;children&quot;: []
320                                        },
321                                        {
322                                            &quot;entity_id&quot;: 10598341,
323                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
324                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
325                                            &quot;columns&quot;: {
326                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
327                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
328                                                &quot;_custom_twr_audit_note_911328&quot;: null,
329                                                &quot;value&quot;: 7219.08
330                                            },
331                                            &quot;children&quot;: []
332                                        },
333                                        {
334                                            &quot;entity_id&quot;: 10598337,
335                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
336                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
337                                            &quot;columns&quot;: {
338                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
339                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
340                                                &quot;_custom_twr_audit_note_911328&quot;: null,
341                                                &quot;value&quot;: 7219.08
342                                            },
343                                            &quot;children&quot;: []
344                                        },
345                                        {
346                                            &quot;entity_id&quot;: 10598334,
347                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
348                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
349                                            &quot;columns&quot;: {
350                                                &quot;time_weighted_return&quot;: -0.039799855483646174,
351                                                &quot;time_weighted_return_2&quot;: -0.039799855483646174,
352                                                &quot;_custom_twr_audit_note_911328&quot;: null,
353                                                &quot;value&quot;: 7219.08
354                                            },
355                                            &quot;children&quot;: []
356                                        }
357                                    ]
358                                },
359                                {
360                                    &quot;name&quot;: &quot;Asset Class C&quot;,
361                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
362                                    &quot;columns&quot;: {
363                                        &quot;time_weighted_return&quot;: -0.03037038746301135,
364                                        &quot;time_weighted_return_2&quot;: -0.03037038746301135,
365                                        &quot;_custom_twr_audit_note_911328&quot;: null,
366                                        &quot;value&quot;: 114472.69744
367                                    },
368                                    &quot;children&quot;: [{
369                                            &quot;entity_id&quot;: 10868778,
370                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
371                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
372                                            &quot;columns&quot;: {
373                                                &quot;time_weighted_return&quot;: -0.030370390035505124,
374                                                &quot;time_weighted_return_2&quot;: -0.030370390035505124,
375                                                &quot;_custom_twr_audit_note_911328&quot;: null,
376                                                &quot;value&quot;: 114472.68744000001
377                                            },
378                                            &quot;children&quot;: []
379                                        },
380                                        {
381                                            &quot;entity_id&quot;: 10643052,
382                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
383                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
384                                            &quot;columns&quot;: {
385                                                &quot;time_weighted_return&quot;: 0,
386                                                &quot;time_weighted_return_2&quot;: 0,
387                                                &quot;_custom_twr_audit_note_911328&quot;: null,
388                                                &quot;value&quot;: 0.01
389                                            },
390                                            &quot;children&quot;: []
391                                        }
392                                    ]
393                                },
394                                {
395                                    &quot;name&quot;: &quot;Asset Class D&quot;,
396                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
397                                    &quot;columns&quot;: {
398                                        &quot;time_weighted_return&quot;: -0.06604362523792162,
399                                        &quot;time_weighted_return_2&quot;: -0.06604362523792162,
400                                        &quot;_custom_twr_audit_note_911328&quot;: null,
401                                        &quot;value&quot;: 5722529.229999997
402                                    },
403                                    &quot;children&quot;: [{
404                                            &quot;entity_id&quot;: 10868778,
405                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
406                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
407                                            &quot;columns&quot;: {
408                                                &quot;time_weighted_return&quot;: -0.06154960593668424,
409                                                &quot;time_weighted_return_2&quot;: -0.06154960593668424,
410                                                &quot;_custom_twr_audit_note_911328&quot;: null,
411                                                &quot;value&quot;: 1191838.9399999995
412                                            },
413                                            &quot;children&quot;: []
414                                        },
415                                        {
416                                            &quot;entity_id&quot;: 10643052,
417                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
418                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
419                                            &quot;columns&quot;: {
420                                                &quot;time_weighted_return&quot;: -0.06750460387418267,
421                                                &quot;time_weighted_return_2&quot;: -0.06750460387418267,
422                                                &quot;_custom_twr_audit_note_911328&quot;: null,
423                                                &quot;value&quot;: 4416618.520000002
424                                            },
425                                            &quot;children&quot;: []
426                                        },
427                                        {
428                                            &quot;entity_id&quot;: 10598341,
429                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
430                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
431                                            &quot;columns&quot;: {
432                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
433                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
434                                                &quot;_custom_twr_audit_note_911328&quot;: null,
435                                                &quot;value&quot;: 38190.33
436                                            },
437                                            &quot;children&quot;: []
438                                        },
439                                        {
440                                            &quot;entity_id&quot;: 10598337,
441                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
442                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
443                                            &quot;columns&quot;: {
444                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
445                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
446                                                &quot;_custom_twr_audit_note_911328&quot;: null,
447                                                &quot;value&quot;: 37940.72
448                                            },
449                                            &quot;children&quot;: []
450                                        },
451                                        {
452                                            &quot;entity_id&quot;: 10598334,
453                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
454                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
455                                            &quot;columns&quot;: {
456                                                &quot;time_weighted_return&quot;: -0.05604507809250081,
457                                                &quot;time_weighted_return_2&quot;: -0.05604507809250081,
458                                                &quot;_custom_twr_audit_note_911328&quot;: null,
459                                                &quot;value&quot;: 37940.72
460                                            },
461                                            &quot;children&quot;: []
462                                        }
463                                    ]
464                                },
465                                {
466                                    &quot;name&quot;: &quot;Asset Class E&quot;,
467                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
468                                    &quot;columns&quot;: {
469                                        &quot;time_weighted_return&quot;: -0.017118805423322003,
470                                        &quot;time_weighted_return_2&quot;: -0.017118805423322003,
471                                        &quot;_custom_twr_audit_note_911328&quot;: null,
472                                        &quot;value&quot;: 3148495.0914600003
473                                    },
474                                    &quot;children&quot;: [{
475                                            &quot;entity_id&quot;: 10868778,
476                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
477                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
478                                            &quot;columns&quot;: {
479                                                &quot;time_weighted_return&quot;: -0.015251157805867277,
480                                                &quot;time_weighted_return_2&quot;: -0.015251157805867277,
481                                                &quot;_custom_twr_audit_note_911328&quot;: null,
482                                                &quot;value&quot;: 800493.06146
483                                            },
484                                            &quot;children&quot;: []
485                                        },
486                                        {
487                                            &quot;entity_id&quot;: 10643052,
488                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
489                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
490                                            &quot;columns&quot;: {
491                                                &quot;time_weighted_return&quot;: -0.01739609576880241,
492                                                &quot;time_weighted_return_2&quot;: -0.01739609576880241,
493                                                &quot;_custom_twr_audit_note_911328&quot;: null,
494                                                &quot;value&quot;: 2215511.2700000005
495                                            },
496                                            &quot;children&quot;: []
497                                        },
498                                        {
499                                            &quot;entity_id&quot;: 10598341,
500                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Shirley&quot;,
501                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
502                                            &quot;columns&quot;: {
503                                                &quot;time_weighted_return&quot;: -0.02085132265594647,
504                                                &quot;time_weighted_return_2&quot;: -0.02085132265594647,
505                                                &quot;_custom_twr_audit_note_911328&quot;: null,
506                                                &quot;value&quot;: 44031.21
507                                            },
508                                            &quot;children&quot;: []
509                                        },
510                                        {
511                                            &quot;entity_id&quot;: 10598337,
512                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Hannah&quot;,
513                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
514                                            &quot;columns&quot;: {
515                                                &quot;time_weighted_return&quot;: -0.02089393244695803,
516                                                &quot;time_weighted_return_2&quot;: -0.02089393244695803,
517                                                &quot;_custom_twr_audit_note_911328&quot;: null,
518                                                &quot;value&quot;: 44394.159999999996
519                                            },
520                                            &quot;children&quot;: []
521                                        },
522                                        {
523                                            &quot;entity_id&quot;: 10598334,
524                                            &quot;name&quot;: &quot;Cht 11th Tr HBO Lau&quot;,
525                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
526                                            &quot;columns&quot;: {
527                                                &quot;time_weighted_return&quot;: -0.020607507059866248,
528                                                &quot;time_weighted_return_2&quot;: -0.020607507059866248,
529                                                &quot;_custom_twr_audit_note_911328&quot;: null,
530                                                &quot;value&quot;: 44065.39000000001
531                                            },
532                                            &quot;children&quot;: []
533                                        }
534                                    ]
535                                },
536                                {
537                                    &quot;name&quot;: &quot;Asset Class F&quot;,
538                                    &quot;grouping&quot;: &quot;_custom_level_2_624287&quot;,
539                                    &quot;columns&quot;: {
540                                        &quot;time_weighted_return&quot;: -0.0014710489231547497,
541                                        &quot;time_weighted_return_2&quot;: -0.0014710489231547497,
542                                        &quot;_custom_twr_audit_note_911328&quot;: null,
543                                        &quot;value&quot;: 198260.12
544                                    },
545                                    &quot;children&quot;: [{
546                                            &quot;entity_id&quot;: 10868778,
547                                            &quot;name&quot;: &quot;2012 Desc Tr HBO Thalia&quot;,
548                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
549                                            &quot;columns&quot;: {
550                                                &quot;time_weighted_return&quot;: -0.0014477244560456848,
551                                                &quot;time_weighted_return_2&quot;: -0.0014477244560456848,
552                                                &quot;_custom_twr_audit_note_911328&quot;: null,
553                                                &quot;value&quot;: 44612.33
554                                            },
555                                            &quot;children&quot;: []
556                                        },
557                                        {
558                                            &quot;entity_id&quot;: 10643052,
559                                            &quot;name&quot;: &quot;2013 Irrev Tr HBO Thalia&quot;,
560                                            &quot;grouping&quot;: &quot;legal_entity&quot;,
561                                            &quot;columns&quot;: {
562                                                &quot;time_weighted_return&quot;: -0.001477821083437858,
563                                                &quot;time_weighted_return_2&quot;: -0.001477821083437858,
564                                                &quot;_custom_twr_audit_note_911328&quot;: null,
565                                                &quot;value&quot;: 153647.78999999998
566                                            },
567                                            &quot;children&quot;: []
568                                        }
569                                    ]
570                                }
571                            ]
572                        }
573                    ]
574                }]
575            }
576        },
577        &quot;included&quot;: []
578    }
579}
580# Function to read API response / JSON Object
581def response_writer():
582    with open('api_response_2022-02-13.json') as f:
583        api_response = json.load(f)
584        return api_response
585
586# Function to unpack JSON response into pandas dataframe.
587def unpack_response():
588    while True:
589        try:
590            api_response = response_writer()
591            portfolio_views_children = api_response['data']['attributes']['total']['children']
592            portfolios = []
593            for portfolio in portfolio_views_children:
594                entity_columns = []
595                # include portfolio itself within an iterable so the total is the header
596                for entity in itertools.chain([portfolio], portfolio[&quot;children&quot;]):
597                    entity_data = entity[&quot;columns&quot;].copy()  # don't mutate original response
598                    entity_data[&quot;portfolio&quot;] = portfolio[&quot;name&quot;]   # from outer
599                    entity_data[&quot;name&quot;]      = entity[&quot;name&quot;]
600                    entity_columns.append(entity_data)
601
602                df = pd.DataFrame(entity_columns)
603                portfolios.append(df)
604
605            # combine dataframes
606            df = pd.concat(portfolios)
607            # reorder and rename
608            column_ordering = {&quot;portfolio&quot;: &quot;portfolio&quot;, &quot;name&quot;: &quot;name&quot;}
609            column_ordering.update({c[&quot;key&quot;]: c[&quot;display_name&quot;] for c in api_response[&quot;meta&quot;][&quot;columns&quot;]})
610            df = df[column_ordering.keys()]   # beware: un-named cols will be dropped
611            df = df.rename(columns=column_ordering)
612            break
613        except KeyError:
614            print(&quot;-----------------------------------\n&quot;,&quot;API TIMEOUT ERROR: TRY AGAIN...&quot;, &quot;\n-----------------------------------\n&quot;)
615    return df
616unpack_response()
617def process_json(api_response): 
618    
619    def get_column_values(df):    
620        return pd.concat([df, pd.json_normalize(df.pop('columns')).set_axis(df.index)], axis=1)
621    
622    def expand_children(df):
623        if len(df.index) &gt; 1:
624            df['children'] = df['children'].fillna('').apply(lambda x: None if len(x) == 0 else x)
625        df_children = df.pop('children').dropna().explode()
626        if len(df_children.index) == 0: # return df if no children to append
627            return df.index.names, df
628        df_children = pd.json_normalize(df_children, max_level=0).set_axis(df_children.index).set_index('name', append=True)
629        df_children = get_column_values(df_children)
630        idx_names = list(df_children.index.names)
631        idx_names[-1] = idx_names[-1] + '_' + str(len(idx_names))
632        df[idx_names[-1]] = None
633        return idx_names, pd.concat([df.set_index(idx_names[-1], append=True), df_children], axis=0)    
634    
635    columns_dict = pd.DataFrame(api_response['meta']['columns']).set_index('key').to_dict(orient='index') # save column definitions
636    df = pd.DataFrame(api_response['data']['attributes']['total']['children']).set_index('name') # get initial dataframe     
637    df = get_column_values(df) # get columns for initial level
638    
639    # expand children
640    while 'children' in df.columns:
641        idx_names, df = expand_children(df)
642    
643    # reorder/replace column headers and sort index
644    df = (df.loc[:, [x for x in df.columns if x not in columns_dict.keys()] + list(columns_dict.keys())]
645          .rename(columns={k:v['display_name'] for k,v in columns_dict.items()})
646          .sort_index(na_position='first').reset_index())
647    
648    #collapse &quot;name&quot; columns (careful of potential duplicate rows)  
649    for col in idx_names[::-1]:
650            df[idx_names[-1]] = df[idx_names[-1]].fillna(df[col])
651    df = df.rename(columns={'name': 'portfolio', idx_names[-1]: 'name'}).drop(columns=idx_names[1:-1])      
652    
653    return df
654process_json(api_response)
65554.2 ms ± 7.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
656
657unpack_response(api_response) # iterrows
65884.3 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
659

Source https://stackoverflow.com/questions/71104848

QUESTION

AttributeError: Can't get attribute 'new_block' on &lt;module 'pandas.core.internals.blocks'&gt;

Asked 2022-Feb-25 at 13:18

I was using pyspark on AWS EMR (4 r5.xlarge as 4 workers, each has one executor and 4 cores), and I got AttributeError: Can't get attribute 'new_block' on <module 'pandas.core.internals.blocks'. Below is a snippet of the code that threw this error:

1search =  SearchEngine(db_file_dir = &quot;/tmp/db&quot;)
2conn = sqlite3.connect(&quot;/tmp/db/simple_db.sqlite&quot;)
3pdf_ = pd.read_sql_query('''select  zipcode, lat, lng, 
4                        bounds_west, bounds_east, bounds_north, bounds_south from 
5                        simple_zipcode''',conn)
6brd_pdf = spark.sparkContext.broadcast(pdf_) 
7conn.close()
8
9
10@udf('string')
11def get_zip_b(lat, lng):
12    pdf = brd_pdf.value 
13    out = pdf[(np.array(pdf[&quot;bounds_north&quot;]) &gt;= lat) &amp; 
14              (np.array(pdf[&quot;bounds_south&quot;]) &lt;= lat) &amp; 
15              (np.array(pdf['bounds_west']) &lt;= lng) &amp; 
16              (np.array(pdf['bounds_east']) &gt;= lng) ]
17    if len(out):
18        min_index = np.argmin( (np.array(out[&quot;lat&quot;]) - lat)**2 + (np.array(out[&quot;lng&quot;]) - lng)**2)
19        zip_ = str(out[&quot;zipcode&quot;].iloc[min_index])
20    else:
21        zip_ = 'bad'
22    return zip_
23
24df = df.withColumn('zipcode', get_zip_b(col(&quot;latitude&quot;),col(&quot;longitude&quot;)))
25

Below is the traceback, where line 102, in get_zip_b refers to pdf = brd_pdf.value:

1search =  SearchEngine(db_file_dir = &quot;/tmp/db&quot;)
2conn = sqlite3.connect(&quot;/tmp/db/simple_db.sqlite&quot;)
3pdf_ = pd.read_sql_query('''select  zipcode, lat, lng, 
4                        bounds_west, bounds_east, bounds_north, bounds_south from 
5                        simple_zipcode''',conn)
6brd_pdf = spark.sparkContext.broadcast(pdf_) 
7conn.close()
8
9
10@udf('string')
11def get_zip_b(lat, lng):
12    pdf = brd_pdf.value 
13    out = pdf[(np.array(pdf[&quot;bounds_north&quot;]) &gt;= lat) &amp; 
14              (np.array(pdf[&quot;bounds_south&quot;]) &lt;= lat) &amp; 
15              (np.array(pdf['bounds_west']) &lt;= lng) &amp; 
16              (np.array(pdf['bounds_east']) &gt;= lng) ]
17    if len(out):
18        min_index = np.argmin( (np.array(out[&quot;lat&quot;]) - lat)**2 + (np.array(out[&quot;lng&quot;]) - lng)**2)
19        zip_ = str(out[&quot;zipcode&quot;].iloc[min_index])
20    else:
21        zip_ = 'bad'
22    return zip_
23
24df = df.withColumn('zipcode', get_zip_b(col(&quot;latitude&quot;),col(&quot;longitude&quot;)))
2521/08/02 06:18:19 WARN TaskSetManager: Lost task 12.0 in stage 7.0 (TID 1814, ip-10-22-17-94.pclc0.merkle.local, executor 6): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
26  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/worker.py&quot;, line 605, in main
27    process()
28  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/worker.py&quot;, line 597, in process
29    serializer.dump_stream(out_iter, outfile)
30  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/serializers.py&quot;, line 223, in dump_stream
31    self.serializer.dump_stream(self._batched(iterator), stream)
32  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/serializers.py&quot;, line 141, in dump_stream
33    for obj in iterator:
34  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/serializers.py&quot;, line 212, in _batched
35    for item in iterator:
36  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/worker.py&quot;, line 450, in mapper
37    result = tuple(f(*[a[o] for o in arg_offsets]) for (arg_offsets, f) in udfs)
38  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/worker.py&quot;, line 450, in &lt;genexpr&gt;
39    result = tuple(f(*[a[o] for o in arg_offsets]) for (arg_offsets, f) in udfs)
40  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/worker.py&quot;, line 90, in &lt;lambda&gt;
41    return lambda *a: f(*a)
42  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/util.py&quot;, line 121, in wrapper
43    return f(*args, **kwargs)
44  File &quot;/mnt/var/lib/hadoop/steps/s-1IBFS0SYWA19Z/Mobile_ID_process_center.py&quot;, line 102, in get_zip_b
45  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/broadcast.py&quot;, line 146, in value
46    self._value = self.load_from_path(self._path)
47  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/broadcast.py&quot;, line 123, in load_from_path
48    return self.load(f)
49  File &quot;/mnt/yarn/usercache/hadoop/appcache/application_1627867699893_0001/container_1627867699893_0001_01_000009/pyspark.zip/pyspark/broadcast.py&quot;, line 129, in load
50    return pickle.load(file)
51AttributeError: Can't get attribute 'new_block' on &lt;module 'pandas.core.internals.blocks' from '/mnt/miniconda/lib/python3.9/site-packages/pandas/core/internals/blocks.py'&gt;
52

Some observations and thought process:

1, After doing some search online, the AttributeError in pyspark seems to be caused by mismatched pandas versions between driver and workers?

2, But I ran the same code on two different datasets, one worked without any errors but the other didn't, which seems very strange and undeterministic, and it seems like the errors may not be caused by mismatched pandas versions. Otherwise, neither two datasets would succeed.

3, I then ran the same code on the successful dataset again, but this time with different spark configurations: setting spark.driver.memory from 2048M to 4192m, and it threw AttributeError.

4, In conclusion, I think the AttributeError has something to do with driver. But I can't tell how they are related from the error message, and how to fix it: AttributeError: Can't get attribute 'new_block' on <module 'pandas.core.internals.blocks'.

ANSWER

Answered 2021-Aug-26 at 14:53

I had the same error using pandas 1.3.2 in the server while 1.2 in my client. Downgrading pandas to 1.2 solved the problem.

Source https://stackoverflow.com/questions/68625748

QUESTION

How to update pandas DataFrame.drop() for Future Warning - all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only

Asked 2022-Feb-13 at 19:56

The following code:

1df = df.drop('market', 1)
2

generates the warning:

FutureWarning: In a future version of pandas all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only

market is the column we want to drop, and we pass the 1 as a second parameter for axis (0 for index, 1 for columns, so we pass 1).

How can we change this line of code now so that it is not a problem in the future version of pandas / to resolve the warning message now?

ANSWER

Answered 2022-Feb-13 at 19:56

From the documentation, pandas.DataFrame.drop has the following parameters:

Parameters

  • labels: single label or list-like Index or column labels to drop.

  • axis: {0 or ‘index’, 1 or ‘columns’}, default 0 Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).

  • index: single label or list-like Alternative to specifying axis (labels, axis=0 is equivalent to index=labels).

  • columns: single label or list-like Alternative to specifying axis (labels, axis=1 is equivalent to columns=labels).

  • level: int or level name, optional For MultiIndex, level from which the labels will be removed.

  • inplace: bool, default False If False, return a copy. Otherwise, do operation inplace and return None.

  • errors: {‘ignore’, ‘raise’}, default ‘raise’ If ‘ignore’, suppress error and only existing labels are dropped.

Moving forward, only labels (the first parameter) can be positional.


So, for this example, the drop code should be as follows:

1df = df.drop('market', 1)
2df = df.drop('market', axis=1)
3

or (more legibly) with columns:

1df = df.drop('market', 1)
2df = df.drop('market', axis=1)
3df = df.drop(columns='market')
4

Source https://stackoverflow.com/questions/68900763

QUESTION

Cannot set up a conda environment with python 3.10

Asked 2022-Jan-31 at 10:35

I am trying to set up a conda environment with python 3.10 installed. For some reason, no install commands for additional packages are working. For example, if I run conda install pandas, I get the error:

1PackagesNotFoundError: The following packages are not available from current channels:
2
3  - python=3.1
4

conda install -c conda-forge pandas doesn't work either. Not sure what the problem is.

ANSWER

Answered 2021-Oct-08 at 08:42

Thats a bug in conda, you can read more about it here: https://github.com/conda/conda/issues/10969

Right now there is a PR to fix it but its not a released version. For now, just stick with

1PackagesNotFoundError: The following packages are not available from current channels:
2
3  - python=3.1
4conda install python=3.9
5

Source https://stackoverflow.com/questions/69481608

QUESTION

ImportError: cannot import name 'ABCIndexClass' from 'pandas.core.dtypes.generic'

Asked 2022-Jan-12 at 23:01

I have this output :

[Pandas-profiling] ImportError: cannot import name 'ABCIndexClass' from 'pandas.core.dtypes.generic'

when trying to import pandas-profiling in this fashion :

1from pandas_profiling import ProfileReport
2

It seems to import pandas-profiling correctly but struggles when it comes to interfacing with pandas itself. Both libraries are currently up to date through conda. It doesn't seem to match any of the common problems associated with pandas-profiling as per their documentation, and I can't seem to locate a more general solution of importing the name ABCIndexClass.

Thanks

ANSWER

Answered 2021-Aug-09 at 19:19

Pandas v1.3 renamed the ABCIndexClass to ABCIndex. The visions dependency of the pandas-profiling package hasn't caught up yet, and so throws an error when it can't find ABCIndexClass. Downgrading pandas to the 1.2.x series will resolve the issue.

Alternatively, you can just wait for the visions package to be updated.

Source https://stackoverflow.com/questions/68704002

QUESTION

Merge two pandas DataFrame based on partial match

Asked 2022-Jan-06 at 00:54

Two DataFrames have city names that are not formatted the same way. I'd like to do a Left-outer join and pull geo field for all partial string matches between the field City in both DataFrames.

1import pandas as pd
2
3df1 = pd.DataFrame({
4                    'City': ['San Francisco, CA','Oakland, CA'], 
5                    'Val': [1,2]
6                  })
7
8df2 = pd.DataFrame({
9                    'City': ['San Francisco-Oakland, CA','Salinas, CA'], 
10                    'Geo': ['geo1','geo2']
11                  })
12

Expected DataFrame upon join:

1import pandas as pd
2
3df1 = pd.DataFrame({
4                    'City': ['San Francisco, CA','Oakland, CA'], 
5                    'Val': [1,2]
6                  })
7
8df2 = pd.DataFrame({
9                    'City': ['San Francisco-Oakland, CA','Salinas, CA'], 
10                    'Geo': ['geo1','geo2']
11                  })
12 City                   Val   Geo
13
14 San Francisco, CA      1     geo1
15 Oakland, CA            2     geo1
16

ANSWER

Answered 2021-Sep-12 at 20:24

This should do the job. String match with Levenshtein_distance.

pip install thefuzz[speedup]

1import pandas as pd
2
3df1 = pd.DataFrame({
4                    'City': ['San Francisco, CA','Oakland, CA'], 
5                    'Val': [1,2]
6                  })
7
8df2 = pd.DataFrame({
9                    'City': ['San Francisco-Oakland, CA','Salinas, CA'], 
10                    'Geo': ['geo1','geo2']
11                  })
12 City                   Val   Geo
13
14 San Francisco, CA      1     geo1
15 Oakland, CA            2     geo1
16import pandas as pd
17import numpy as np
18
19from thefuzz import process
20
21def fuzzy_match(
22    a: pd.DataFrame, b: pd.DataFrame, col: str, limit: int = 5, thresh: int = 80
23):
24    &quot;&quot;&quot;use fuzzy matching to join on column&quot;&quot;&quot;
25
26    s = b[col].tolist()
27
28    matches = a[col].apply(lambda x: process.extract(x, s, limit=limit))
29    matches = pd.DataFrame(np.concatenate(matches), columns=[&quot;match&quot;, &quot;score&quot;])
30
31    # join other columns in b to matches
32    to_join = (
33        pd.merge(left=b, right=matches, how=&quot;right&quot;, left_on=&quot;City&quot;, right_on=&quot;match&quot;)
34        .set_index(  # create an index that represents the matching row in df a, you can drop this when `limit=1`
35            np.array(
36                list(
37                    np.repeat(i, limit if limit &lt; len(b) else len(b))
38                    for i in range(len(a))
39                )
40            ).flatten()
41        )
42        .drop(columns=[&quot;match&quot;])
43        .astype({&quot;score&quot;: &quot;int16&quot;})
44    )
45    print(f&quot;\t the index here represents the row in dataframe a on which to join&quot;)
46    print(to_join)
47
48    res = pd.merge(
49        left=a, right=to_join, left_index=True, right_index=True, suffixes=(&quot;&quot;, &quot;_b&quot;)
50    )
51
52    # return only the highest match or you can just set the limit to 1
53    # and remove this
54    df = res.reset_index()
55    df = df.iloc[df.groupby(by=&quot;index&quot;)[&quot;score&quot;].idxmax()].reset_index(drop=True)
56
57    return df.drop(columns=[&quot;City_b&quot;, &quot;score&quot;, &quot;index&quot;])
58
59def test(df):
60
61    expected = pd.DataFrame(
62        {
63            &quot;City&quot;: [&quot;San Francisco, CA&quot;, &quot;Oakland, CA&quot;],
64            &quot;Val&quot;: [1, 2],
65            &quot;Geo&quot;: [&quot;geo1&quot;, &quot;geo1&quot;],
66        }
67    )
68
69    print(f'{&quot;expected&quot;:-^70}')
70    print(expected)
71
72    print(f'{&quot;res&quot;:-^70}')
73    print(df)
74
75    assert expected.equals(df)
76
77
78if __name__ == &quot;__main__&quot;:
79
80    a = pd.DataFrame({&quot;City&quot;: [&quot;San Francisco, CA&quot;, &quot;Oakland, CA&quot;], &quot;Val&quot;: [1, 2]})
81    b = pd.DataFrame(
82        {&quot;City&quot;: [&quot;San Francisco-Oakland, CA&quot;, &quot;Salinas, CA&quot;], &quot;Geo&quot;: [&quot;geo1&quot;, &quot;geo2&quot;]}
83    )
84
85    print(f'\n\n{&quot;fuzzy match&quot;:-^70}')
86    res = fuzzy_match(a, b, col=&quot;City&quot;)
87    test(res)
88
89

Source https://stackoverflow.com/questions/69125666

QUESTION

Create a new column in a Pandas DataFrame from existing column names

Asked 2021-Nov-15 at 00:22

I want to deconstruct a pandas DataFrame, using column headers as a new data-column and create a list with all combinations of the row index and columns. Easier to show than explain:

1index_col = [&quot;store1&quot;, &quot;store2&quot;, &quot;store3&quot;]
2cols = [&quot;January&quot;, &quot;February&quot;, &quot;March&quot;]
3values = [[2,3,4],[5,6,7],[8,9,10]]
4df = pd.DataFrame(values, index=index_col, columns=cols)
5

From this DataFrame I wish to get the following list:

1index_col = [&quot;store1&quot;, &quot;store2&quot;, &quot;store3&quot;]
2cols = [&quot;January&quot;, &quot;February&quot;, &quot;March&quot;]
3values = [[2,3,4],[5,6,7],[8,9,10]]
4df = pd.DataFrame(values, index=index_col, columns=cols)
5[['store1', 'January', 2],
6 ['store1', 'February', 3],
7 ['store1', 'March', 4],
8 ['store2', 'January', 5],
9 ['store2', 'February', 6],
10 ['store2', 'March', 7],
11 ['store3', 'January', 8],
12 ['store3', 'February', 9],
13 ['store3', 'March', 10]]
14

Is there a convenient way to do this?

ANSWER

Answered 2021-Nov-09 at 23:58

The structure that you want your data in is very messy, so this is probably the best method given the data you want.

1index_col = [&quot;store1&quot;, &quot;store2&quot;, &quot;store3&quot;]
2cols = [&quot;January&quot;, &quot;February&quot;, &quot;March&quot;]
3values = [[2,3,4],[5,6,7],[8,9,10]]
4df = pd.DataFrame(values, index=index_col, columns=cols)
5[['store1', 'January', 2],
6 ['store1', 'February', 3],
7 ['store1', 'March', 4],
8 ['store2', 'January', 5],
9 ['store2', 'February', 6],
10 ['store2', 'March', 7],
11 ['store3', 'January', 8],
12 ['store3', 'February', 9],
13 ['store3', 'March', 10]]
14# Results
15res = []
16
17# Nested loop: first for length of index col, then next for cols
18for i in range(len(index_col)):
19    for j in range(len(cols)):
20        # Format of data
21        res.append([index_col[i], cols[j], values[i][j]])
22
23# Return results
24print(res)
25return res
26

Source https://stackoverflow.com/questions/69906411

QUESTION

After conda update, python kernel crashes when matplotlib is used

Asked 2021-Nov-06 at 19:03

I have create this simple env with conda:

1conda create -n test python=3.8.5 pandas scipy numpy matplotlib seaborn jupyterlab
2

The following code in jupyter lab crashes the kernel :

1conda create -n test python=3.8.5 pandas scipy numpy matplotlib seaborn jupyterlab
2import matplotlib.pyplot as plt
3
4plt.subplot()
5

I don't face the problem on Linux. The problem is when I try on Windows 10.

There are no errors on the jupyter lab console (where I started the server), and I have no idea where to investigate.

ANSWER

Answered 2021-Nov-06 at 19:03
Update 2021-11-06
  • The default pkgs/main channel for conda has reverted to using freetype 2.10.4 for Windows, per main / packages / freetype.
  • If you are still experiencing the issue, use conda list freetype to check the version: freetype != 2.11.0
    • If it is 2.11.0, then change the version per the solution, or conda update --all (providing your default channel isn't changed in the .condarc config file).
Solution
  • If this is occurring after installing Anaconda, updating conda or freetype since Oct 27, 2021.
  • Go to the Anaconda prompt and downgrade freetype 2.11.0 in any affected environment.
    • conda install freetype=2.10.4
  • Relevant to any package using matplotlib and any IDE
    • For example, pandas.DataFrame.plot and seaborn
    • Jupyter, Spyder, VSCode, PyCharm, command line.

Discovery
  • An issue occurs after updating with the most current updates from conda, released Friday, Oct 29.
  • After updating with conda update --all, there's an issue with anything related to matplotlib in any IDE (not just Jupyter).
    • I tested this in JupyterLab, PyCharm, and python from the command prompt.
    • PyCharm: Process finished with exit code -1073741819
    • JupyterLab: kernel just restarts and there are no associated errors or Traceback
    • command prompt: a blank interactive matplotlib window will appear briefly, and then a new command line appears.
  • The issue seems to be with conda update --all in (base), then any plot API that uses matplotlib (e.g. seaborn and pandas.DataFrame.plot) kills the kernel in any environment.
  • I had to reinstall Anaconda, but do not do an update of (base), then my other environments worked.
  • I have not figured out what specifically is causing the issue.
  • I tested the issue with python 3.8.12 and python 3.9.7
  • Current Testing:
    • Following is the conda revision log.
    • Prior to conda update --all this environment was working, but after the updates, plotting with matplotlib crashes the python kernel
1conda create -n test python=3.8.5 pandas scipy numpy matplotlib seaborn jupyterlab
2import matplotlib.pyplot as plt
3
4plt.subplot()
5  2021-10-31 10:47:22  (rev 3)
6     bokeh  {2.3.3 (defaults/win-64) -&gt; 2.4.1 (defaults/win-64)}
7     click  {8.0.1 (defaults/noarch) -&gt; 8.0.3 (defaults/noarch)}
8     filelock  {3.0.12 (defaults/noarch) -&gt; 3.3.1 (defaults/noarch)}
9     freetype  {2.10.4 (defaults/win-64) -&gt; 2.11.0 (defaults/win-64)}
10     imagecodecs  {2021.6.8 (defaults/win-64) -&gt; 2021.8.26 (defaults/win-64)}
11     joblib  {1.0.1 (defaults/noarch) -&gt; 1.1.0 (defaults/noarch)}
12     lerc  {2.2.1 (defaults/win-64) -&gt; 3.0 (defaults/win-64)}
13     more-itertools  {8.8.0 (defaults/noarch) -&gt; 8.10.0 (defaults/noarch)}
14     pyopenssl  {20.0.1 (defaults/noarch) -&gt; 21.0.0 (defaults/noarch)}
15     scikit-learn  {0.24.2 (defaults/win-64) -&gt; 1.0.1 (defaults/win-64)}
16     statsmodels  {0.12.2 (defaults/win-64) -&gt; 0.13.0 (defaults/win-64)}
17     sympy  {1.8 (defaults/win-64) -&gt; 1.9 (defaults/win-64)}
18     tqdm  {4.62.2 (defaults/noarch) -&gt; 4.62.3 (defaults/noarch)}
19     xlwings  {0.24.7 (defaults/win-64) -&gt; 0.24.9 (defaults/win-64)}
20
  • The issue seems to be freetype
    • Downgrading from 2.11.0 to 2.10.4 resolved the issue and made the environment work with matplotlib
  • Went to post a bug report and discovered there is [Bug]: Matplotlib crashes Python #21511

Source https://stackoverflow.com/questions/69786885

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Pandas

Share this Page

share link

Get latest updates on Pandas