Skip to content
Advertisement

Convert nested dict to dataframe, syntax error?

Problem

I am converting multiple nested dicts to dataframes. I have a slightly different dict that I haven’t been able to convert to a dataframe using my attempted solution. I am providing a shortened copy of my dict with dummy values as the reprex.

Reprex dict:

JavaScript

My attempted solution

Code is based on similar dict wrangling problems that I had, but I am not sure how to apply it for this specific dict.

JavaScript

Expected dataframe format:

cur_pearson_target_prediction_correlation cur_pearson_abs_max_features_correlation cur_cramer_v_target_prediction_correlation
None 0.1 None

Error message

I am currently getting this error too.

JavaScript

Advertisement

Answer

As mentioned in the comments, the TypeError is due result['result'] (which is a dictionary) not being usable as a key. If you used some thing [like result['metric']] then the error would no longer be raised, but I think that the resulting structure is not the outcome you want.

For flattening nested data, I often take a recursive approach. Below is a simplified version of my flattenObj function:

JavaScript

JavaScript
rows
JavaScript

rowsDf.T [Transposed to fit better]
opdf

If you don’t want the metric column, you can either drop it or omit it by defining rows as

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement