From Nested dictionary to a flattened Dataframe

Question:

I have a bit of a nightmare here. I tried flatten_dict, MutableMapping, export to json trying to read it and cleaning, etc. Nothing works.

So, I have 720 rows like these:

"{'utilidad neta': 954700.2, 'gastos operacionales': {'total': 32505631.93, 'gastos administrativos': 24734891.6, 'provisión y castigo de cartera': 0.0, 'gastos de venta': 7770740.33, 'costos operativos': 0.0}, 'costo de ventas': {'costo servicios': 0.0, 'total': 20084030.38, 'costo mercancÃxada vendida': 20084030.38}, 'utilidad antes de impuestos': 954700.2, 'otros egresos': {'total': 5026105.03, 'pérdida en venta de activos': 0.0, 'financieros': 5026105.03, 'diferencia en cambio': 0.0, 'donaciones': 0.0}, 'impuestos': {'total': 0.0, 'impuestos sobre la renta': 0.0, 'cree': 0.0}, 'utilidad operacional': -2846492.31, 'ingresos operacionales': {'total': 49743170.0, 'devoluciones en ventas': {'total': -552065.0, 'devoluciones no gravadas': 0.0, 'devoluciones gravadas': -552065.0}, 'ventas y servicios': {'total': 50295235.0, 'ventas no gravadas': 342438.0, 'ventas gravadas': 49952797.0, 'servicios': 0.0}}, 'utilidad bruta': 29659139.62, 'ingresos no operacionales': {'total': 8827297.54, 'financieros': -8.46, 'diferencia en cambio': 0.0, 'otros ingresos': {'otros ingresos - (ganancia en venta de activos)': 0.0, 'total': 8827306.0, 'otros ingresos - reintegro de costos y gastos': 8827306.0}}}"
"{'utilidad neta': -48227366.15, 'gastos operacionales': {'total': 39553354.0, 'gastos administrativos': 39553354.0, 'provisión y castigo de cartera': 0.0, 'gastos de venta': 0.0}, 'costo de ventas': {'costo servicios': 0.0, 'total': 45724691.15, 'costo mercancÃxada vendida': 45724691.15}, 'utilidad antes de impuestos': -48227366.15, 'otros egresos': {'total': 0.0, 'pérdida en venta de activos': 0.0, 'financieros': 0.0, 'diferencia en cambio': 0.0, 'donaciones': 0.0}, 'impuestos': {'total': 0.0, 'impuestos sobre la renta': 0.0, 'cree': 0.0}, 'utilidad operacional': -23738621.15, 'ingresos operacionales': {'total': 61539424.0, 'devoluciones en ventas': -4040000.0, 'ventas y servicios': 65579424.0}, 'utilidad bruta': 15814732.85, 'ingresos no operacionales': {'total': -24488745.0, 'otros ingresos (ganancia en venta de activos)': 0.0, 'financieros': -24488745.0, 'diferencia en cambio': 0.0}}"

The idea is to build a data frame for modeling, so Every item has to create a column with a name related with the parent of the nested dictionary.

The output, it has to be similar to this:

Gastos operacionales_total Gastos operacionales_gastos administrativos
32505631.93 24734891.6
39553354 39553354

As you can see, the problem is that not all the rows have same data. So for each topic, it needs to create a column.

Kindly, can you give me ideas to solve this.

Answers:

Use json_normalize with convert values to dictionaries by literal_eval:

import ast

df = pd.json_normalize(df['col'].apply(ast.literal_eval))

print (df)

   utilidad neta  utilidad antes de impuestos  utilidad operacional  
0      954700.20                    954700.20           -2846492.31   
1   -48227366.15                 -48227366.15          -23738621.15   

   utilidad bruta  gastos operacionales.total  
0     29659139.62                 32505631.93   
1     15814732.85                 39553354.00   

   gastos operacionales.gastos administrativos  
0                                   24734891.6   
1                                   39553354.0   

   gastos operacionales.provisión y castigo de cartera  
0                                                0.0      
1                                                0.0      

   gastos operacionales.gastos de venta  
0                            7770740.33   
1                                  0.00   

   gastos operacionales.costos operativos  costo de ventas.costo servicios  
0                                     0.0                              0.0   
1                                     NaN                              0.0   

   costo de ventas.total  costo de ventas.costo mercancía vendida  
0            20084030.38                               20084030.38   
1            45724691.15                               45724691.15   

   otros egresos.total  otros egresos.pérdida en venta de activos  
0           5026105.03                                         0.0   
1                 0.00                                         0.0   

   otros egresos.financieros  otros egresos.diferencia en cambio  
0                 5026105.03                                 0.0   
1                       0.00                                 0.0   

   otros egresos.donaciones  impuestos.total  
0                       0.0              0.0   
1                       0.0              0.0   

   impuestos.impuestos sobre la renta  impuestos.cree  
0                                 0.0             0.0   
1                                 0.0             0.0   

   ingresos operacionales.total  
0                    49743170.0   
1                    61539424.0   

   ingresos operacionales.devoluciones en ventas.total  
0                                          -552065.0     
1                                                NaN     

   ingresos operacionales.devoluciones en ventas.devoluciones no gravadas  
0                                                0.0                        
1                                                NaN                        

   ingresos operacionales.devoluciones en ventas.devoluciones gravadas  
0                                          -552065.0                     
1                                                NaN                     

   ingresos operacionales.ventas y servicios.total  
0                                       50295235.0   
1                                              NaN   

   ingresos operacionales.ventas y servicios.ventas no gravadas  
0                                           342438.0              
1                                                NaN              

   ingresos operacionales.ventas y servicios.ventas gravadas  
0                                         49952797.0           
1                                                NaN           

   ingresos operacionales.ventas y servicios.servicios  
0                                                0.0     
1                                                NaN     

   ingresos no operacionales.total  ingresos no operacionales.financieros  
0                       8827297.54                                  -8.46   
1                     -24488745.00                           -24488745.00   

   ingresos no operacionales.diferencia en cambio  
0                                             0.0   
1                                             0.0   

   ingresos no operacionales.otros ingresos.otros ingresos - (ganancia en venta de activos)  
0                                                0.0                                          
1                                                NaN                                          

   ingresos no operacionales.otros ingresos.total  
0                                       8827306.0   
1                                             NaN   

   ingresos no operacionales.otros ingresos.otros ingresos - reintegro de costos y gastos  
0                                          8827306.0                                        
1                                                NaN                                        

   ingresos operacionales.devoluciones en ventas  
0                                            NaN   
1                                     -4040000.0   

   ingresos operacionales.ventas y servicios  
0                                        NaN   
1                                 65579424.0   

   ingresos no operacionales.otros ingresos (ganancia en venta de activos)  
0                                                NaN                        
1                                                0.0            
Answered By: jezrael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.