我这里有点噩梦.我try 了flatten\u dict、MutableMapping、导出到json并try 读取它和清理,等等.什么都不管用.

所以,我有720行像这样:

"{'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}}"

其思想是构建一个用于建模的数据框架,因此每个项都必须创建一个名称与嵌套字典的父项相关的列.

输出必须类似于:

Gastos operacionales_total Gastos operacionales_gastos administrativos
32505631.93 24734891.6
39553354 39553354

如您所见,问题在于并非所有行都具有相同的数据.因此,对于每个主题,都需要创建一个列.

好心的,你能给我一些解决这个问题的方法吗.

推荐答案

使用json_normalize将值转换为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            

Python相关问答推荐

如何推迟对没有公钥的视图/表的反射?

计算每月过go x年的平均值

如何在Pygame中绘制右对齐的文本?

两极:如何分割一个大 pyramid 并并行保存每个

具有2D功能的Python十六进制图

过载功能是否包含Support Int而不是Support Int?

无法使用python.h文件; Python嵌入错误

如果索引不存在,pandas系列将通过索引获取值,并填充值

Locust请求中的Python和参数

Polars比较了两个预设-有没有方法在第一次不匹配时立即失败

Python多处理:当我在一个巨大的pandas数据框架上启动许多进程时,程序就会陷入困境

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

从收件箱中的列中删除html格式

在Pandas DataFrame操作中用链接替换'方法的更有效方法

如果条件不满足,我如何获得掩码的第一个索引并获得None?

如何在WSL2中更新Python到最新版本(3.12.2)?

所有列的滚动标准差,忽略NaN

无法在Docker内部运行Python的Matlab SDK模块,但本地没有问题

从列表中获取n个元素,其中list [i][0]== value''

为什么调用函数的值和次数不同,递归在代码中是如何工作的?